test02/node_modules/@vuepress/plugin-prismjs/lib/node/markdown/highlightPlugin.js
罗佳鸿 6aa1ebe342
Some checks are pending
部署文档 / deploy-gh-pages (push) Waiting to run
first commit
2024-08-13 10:11:19 +08:00

41 lines
1.9 KiB
JavaScript

import { getCodeParser, getHighlightLinesRange, highlightCodeLines, metaWhitespace, metaWordHighlight, notationDiff, notationErrorLevel, notationFocus, notationHighlight, notationWordHighlight, } from '../parser/index.js';
import { resolveLanguage } from '../utils/index.js';
export const highlightPlugin = (md, { highlightLines = true, notationDiff: enabledDiff, notationErrorLevel: enabledErrorLevel, notationFocus: enabledFocus, notationHighlight: enabledHighlight, notationWordHighlight: enabledWordHighlight, whitespace: whitespacePosition = false, } = {}) => {
const rawFence = md.renderer.rules.fence;
md.renderer.rules.fence = (...args) => {
const [tokens, idx, { langPrefix }] = args;
const token = tokens[idx];
// get token info
const info = token.info ? md.utils.unescapeAll(token.info).trim() : '';
// resolve language from token info
const language = resolveLanguage(info);
const languageClass = `${langPrefix}${language.name}`;
const code = rawFence(...args)
// remove the default `language-${ext}` class
.replace(/<code[^]*?>/, '<code>');
const parser = getCodeParser(code);
if (highlightLines) {
highlightCodeLines(parser, getHighlightLinesRange(info));
}
if (enabledDiff) {
notationDiff(parser);
}
if (enabledErrorLevel) {
notationErrorLevel(parser);
}
if (enabledFocus) {
notationFocus(parser);
}
if (enabledHighlight) {
notationHighlight(parser);
}
if (enabledWordHighlight) {
notationWordHighlight(parser);
metaWordHighlight(parser, info);
}
metaWhitespace(parser, info, whitespacePosition);
parser.pre.classList.push(languageClass);
return parser.stringify();
};
};