2021-04-14 04:24:52 -04:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
|
|
|
*
|
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-11-12 10:43:40 -05:00
|
|
|
import rangeParser from 'parse-numeric-range';
|
|
|
|
|
|
2022-02-01 04:43:15 -05:00
|
|
|
const codeBlockTitleRegex = /title=(?<quote>["'])(?<title>.*?)\1/;
|
2022-03-13 06:32:17 -04:00
|
|
|
const highlightLinesRangeRegex = /\{(?<range>[\d,-]+)\}/;
|
2021-11-12 10:43:40 -05:00
|
|
|
|
|
|
|
|
const commentTypes = ['js', 'jsBlock', 'jsx', 'python', 'html'] as const;
|
|
|
|
|
type CommentType = typeof commentTypes[number];
|
|
|
|
|
|
|
|
|
|
type CommentPattern = {
|
|
|
|
|
start: string;
|
|
|
|
|
end: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Supported types of highlight comments
|
|
|
|
|
const commentPatterns: Record<CommentType, CommentPattern> = {
|
|
|
|
|
js: {
|
|
|
|
|
start: '\\/\\/',
|
|
|
|
|
end: '',
|
|
|
|
|
},
|
|
|
|
|
jsBlock: {
|
|
|
|
|
start: '\\/\\*',
|
|
|
|
|
end: '\\*\\/',
|
|
|
|
|
},
|
|
|
|
|
jsx: {
|
|
|
|
|
start: '\\{\\s*\\/\\*',
|
|
|
|
|
end: '\\*\\/\\s*\\}',
|
|
|
|
|
},
|
|
|
|
|
python: {
|
|
|
|
|
start: '#',
|
|
|
|
|
end: '',
|
|
|
|
|
},
|
|
|
|
|
html: {
|
|
|
|
|
start: '<!--',
|
|
|
|
|
end: '-->',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const magicCommentDirectives = [
|
|
|
|
|
'highlight-next-line',
|
|
|
|
|
'highlight-start',
|
|
|
|
|
'highlight-end',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const getMagicCommentDirectiveRegex = (
|
|
|
|
|
languages: readonly CommentType[] = commentTypes,
|
|
|
|
|
) => {
|
|
|
|
|
// to be more reliable, the opening and closing comment must match
|
|
|
|
|
const commentPattern = languages
|
|
|
|
|
.map((lang) => {
|
|
|
|
|
const {start, end} = commentPatterns[lang];
|
|
|
|
|
return `(?:${start}\\s*(${magicCommentDirectives.join('|')})\\s*${end})`;
|
|
|
|
|
})
|
|
|
|
|
.join('|');
|
|
|
|
|
// white space is allowed, but otherwise it should be on it's own line
|
|
|
|
|
return new RegExp(`^\\s*(?:${commentPattern})\\s*$`);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// select comment styles based on language
|
|
|
|
|
const magicCommentDirectiveRegex = (lang: string) => {
|
|
|
|
|
switch (lang) {
|
|
|
|
|
case 'js':
|
|
|
|
|
case 'javascript':
|
|
|
|
|
case 'ts':
|
|
|
|
|
case 'typescript':
|
|
|
|
|
return getMagicCommentDirectiveRegex(['js', 'jsBlock']);
|
|
|
|
|
|
|
|
|
|
case 'jsx':
|
|
|
|
|
case 'tsx':
|
|
|
|
|
return getMagicCommentDirectiveRegex(['js', 'jsBlock', 'jsx']);
|
|
|
|
|
|
|
|
|
|
case 'html':
|
|
|
|
|
return getMagicCommentDirectiveRegex(['js', 'jsBlock', 'html']);
|
|
|
|
|
|
|
|
|
|
case 'python':
|
|
|
|
|
case 'py':
|
|
|
|
|
return getMagicCommentDirectiveRegex(['python']);
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
// all comment types
|
|
|
|
|
return getMagicCommentDirectiveRegex();
|
|
|
|
|
}
|
|
|
|
|
};
|
2021-04-14 04:24:52 -04:00
|
|
|
|
|
|
|
|
export function parseCodeBlockTitle(metastring?: string): string {
|
2022-02-01 04:43:15 -05:00
|
|
|
return metastring?.match(codeBlockTitleRegex)?.groups!.title ?? '';
|
2021-04-14 04:24:52 -04:00
|
|
|
}
|
2021-11-12 10:43:40 -05:00
|
|
|
|
2021-12-29 11:17:09 -05:00
|
|
|
export function parseLanguage(className: string): string | undefined {
|
2021-11-12 10:43:40 -05:00
|
|
|
const languageClassName = className
|
2021-12-29 11:17:09 -05:00
|
|
|
.split(' ')
|
2021-11-12 10:43:40 -05:00
|
|
|
.find((str) => str.startsWith('language-'));
|
2021-12-29 11:17:09 -05:00
|
|
|
return languageClassName?.replace(/language-/, '');
|
2021-11-12 10:43:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param metastring The highlight range declared here starts at 1
|
|
|
|
|
* @returns Note: all line numbers start at 0, not 1
|
|
|
|
|
*/
|
|
|
|
|
export function parseLines(
|
|
|
|
|
content: string,
|
|
|
|
|
metastring?: string,
|
2021-12-29 11:17:09 -05:00
|
|
|
language?: string,
|
2021-11-12 10:43:40 -05:00
|
|
|
): {
|
|
|
|
|
highlightLines: number[];
|
|
|
|
|
code: string;
|
|
|
|
|
} {
|
|
|
|
|
let code = content.replace(/\n$/, '');
|
|
|
|
|
// Highlighted lines specified in props: don't parse the content
|
|
|
|
|
if (metastring && highlightLinesRangeRegex.test(metastring)) {
|
2022-02-01 04:43:15 -05:00
|
|
|
const highlightLinesRange = metastring.match(highlightLinesRangeRegex)!
|
2022-03-06 00:09:10 -05:00
|
|
|
.groups!.range!;
|
2021-11-12 10:43:40 -05:00
|
|
|
const highlightLines = rangeParser(highlightLinesRange)
|
|
|
|
|
.filter((n) => n > 0)
|
|
|
|
|
.map((n) => n - 1);
|
|
|
|
|
return {highlightLines, code};
|
|
|
|
|
}
|
|
|
|
|
if (language === undefined) {
|
|
|
|
|
return {highlightLines: [], code};
|
|
|
|
|
}
|
|
|
|
|
const directiveRegex = magicCommentDirectiveRegex(language);
|
|
|
|
|
// go through line by line
|
|
|
|
|
const lines = code.split('\n');
|
|
|
|
|
let highlightBlockStart: number;
|
|
|
|
|
let highlightRange = '';
|
|
|
|
|
// loop through lines
|
|
|
|
|
for (let lineNumber = 0; lineNumber < lines.length; ) {
|
2022-03-06 00:09:10 -05:00
|
|
|
const line = lines[lineNumber]!;
|
2021-11-12 10:43:40 -05:00
|
|
|
const match = line.match(directiveRegex);
|
|
|
|
|
if (match !== null) {
|
|
|
|
|
const directive = match.slice(1).find((item) => item !== undefined);
|
|
|
|
|
switch (directive) {
|
|
|
|
|
case 'highlight-next-line':
|
|
|
|
|
highlightRange += `${lineNumber},`;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'highlight-start':
|
|
|
|
|
highlightBlockStart = lineNumber;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'highlight-end':
|
|
|
|
|
highlightRange += `${highlightBlockStart!}-${lineNumber - 1},`;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
lines.splice(lineNumber, 1);
|
|
|
|
|
} else {
|
|
|
|
|
// lines without directives are unchanged
|
|
|
|
|
lineNumber += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const highlightLines = rangeParser(highlightRange);
|
|
|
|
|
code = lines.join('\n');
|
|
|
|
|
return {highlightLines, code};
|
|
|
|
|
}
|