2021-05-19 03:23:59 +00:00
|
|
|
import remark from 'remark';
|
|
|
|
import github from 'remark-github';
|
|
|
|
|
2020-09-10 19:57:18 +00:00
|
|
|
// Generic replacements/link-breakers
|
|
|
|
export function sanitizeMarkdown(markdown: string): string {
|
|
|
|
let res = markdown;
|
|
|
|
// Put a zero width space after every # followed by a digit
|
|
|
|
res = res.replace(/#(\d)/gi, '#​$1');
|
|
|
|
// Put a zero width space after every @ symbol to prevent unintended hyperlinking
|
|
|
|
res = res.replace(/@/g, '@​');
|
|
|
|
res = res.replace(/(`\[?@)​/g, '$1');
|
|
|
|
res = res.replace(/([a-z]@)​/gi, '$1');
|
|
|
|
res = res.replace(/\/compare\/@​/g, '/compare/@');
|
|
|
|
res = res.replace(/(\(https:\/\/[^)]*?)\.\.\.@​/g, '$1...@');
|
|
|
|
res = res.replace(/([\s(])#(\d+)([)\s]?)/g, '$1#​$2$3');
|
|
|
|
// convert escaped backticks back to `
|
|
|
|
const backTickRe = /`([^/]*?)`/g;
|
|
|
|
res = res.replace(backTickRe, '`$1`');
|
|
|
|
res = res.replace(/`#​(\d+)`/g, '`#$1`');
|
|
|
|
return res;
|
|
|
|
}
|
2021-05-19 03:23:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param content content to process
|
|
|
|
* @param options github options
|
|
|
|
* @returns linkified content
|
|
|
|
*/
|
|
|
|
export async function linkify(
|
|
|
|
content: string,
|
|
|
|
options: github.RemarkGithubOptions
|
|
|
|
): Promise<string> {
|
|
|
|
// https://github.com/syntax-tree/mdast-util-to-markdown#optionsbullet
|
|
|
|
const output = await remark()
|
|
|
|
.use({ settings: { bullet: '-' } })
|
|
|
|
.use(github, { mentionStrong: false, ...options })
|
|
|
|
.process(content);
|
|
|
|
return output.toString();
|
|
|
|
}
|