wikiparser-node
Version:
A Node.js parser for MediaWiki markup with AST
50 lines (49 loc) • 2.53 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseHrAndDoubleUnderscore = void 0;
const hr_1 = require("../src/nowiki/hr");
const doubleUnderscore_1 = require("../src/nowiki/doubleUnderscore");
const heading_1 = require("../src/heading");
/* NOT FOR BROWSER */
const constants_1 = require("../util/constants");
/* NOT FOR BROWSER END */
/**
* 解析`<hr>`和状态开关
* @param {Token} root 根节点
* @param config
* @param accum
*/
const parseHrAndDoubleUnderscore = ({ firstChild: { data }, type, name }, config, accum) => {
const { doubleUnderscore: [insensitive, sensitive, aliases] } = config;
config.insensitiveDoubleUnderscore ??= new Set(insensitive);
config.sensitiveDoubleUnderscore ??= new Set(sensitive);
/__(toc|notoc)__/giu; // eslint-disable-line @typescript-eslint/no-unused-expressions
config.regexHrAndDoubleUnderscore ??= new RegExp(`__(${[...insensitive, ...sensitive].join('|')})__`, 'giu');
if (type !== 'root' && (type !== 'ext-inner' || name !== 'poem')) {
data = `\0${data}`;
}
data = data.replace(/^((?:\0\d+[cno]\x7F)*)(-{4,})/gmu, (_, lead, m) => {
// @ts-expect-error abstract class
new hr_1.HrToken(m, config, accum);
return `${lead}\0${accum.length - 1}r\x7F`;
}).replace(config.regexHrAndDoubleUnderscore, (m, p1) => {
const caseSensitive = config.sensitiveDoubleUnderscore.has(p1), lc = p1.toLowerCase(), caseInsensitive = config.insensitiveDoubleUnderscore.has(lc);
if (caseSensitive || caseInsensitive) {
// @ts-expect-error abstract class
new doubleUnderscore_1.DoubleUnderscoreToken(p1, caseSensitive, config, accum);
return `\0${accum.length - 1}${caseInsensitive && (aliases?.[lc] ?? /* istanbul ignore next */ lc) === 'toc' ? 'u' : 'n'}\x7F`;
}
return m;
});
if (!config.excludes.includes('heading')) {
data = data.replace(/^((?:\0\d+[cn]\x7F)*)(={1,6})(.+)\2((?:\s|\0\d+[cn]\x7F)*)$/gmu, (_, lead, equals, heading, trail) => {
const text = `${lead}\0${accum.length}h\x7F`;
// @ts-expect-error abstract class
new heading_1.HeadingToken(equals.length, [heading, trail], config, accum);
return text;
});
}
return type === 'root' || type === 'ext-inner' && name === 'poem' ? data : data.slice(1);
};
exports.parseHrAndDoubleUnderscore = parseHrAndDoubleUnderscore;
constants_1.parsers['parseHrAndDoubleUnderscore'] = __filename;
;