UNPKG

wikiparser-node

Version:

A Node.js parser for MediaWiki markup with AST

88 lines (87 loc) 3.32 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TableBaseToken = void 0; const attributesParent_1 = require("../../mixin/attributesParent"); const index_1 = __importDefault(require("../../index")); const index_2 = require("../index"); const syntax_1 = require("../syntax"); const attributes_1 = require("../attributes"); /* NOT FOR BROWSER */ const debug_1 = require("../../util/debug"); const constants_1 = require("../../util/constants"); /** * 转义表格语法 * @param syntax 表格语法节点 */ const escapeTable = (syntax) => { const wikitext = syntax.childNodes.map(child => child.type === 'text' ? child.data.replace(/\|\|/gu, '{{!!}}') .replace(/\|/gu, '{{!}}') : child.toString()).join(''), { childNodes } = index_1.default .parse(wikitext, syntax.getAttribute('include'), 2, syntax.getAttribute('config')); syntax.safeReplaceChildren(childNodes); }; /** * table row that contains the newline at the beginning but not at the end * * 表格行,含开头的换行,不含结尾的换行 * @classdesc `{childNodes: [SyntaxToken, AttributesToken, ...Token[]]}` */ class TableBaseToken extends (0, attributesParent_1.attributesParent)(1)(index_2.Token) { /* NOT FOR BROWSER END */ /** * @param pattern 表格语法正则 * @param syntax 表格语法 * @param type 节点类型 * @param attr 表格属性 */ constructor(pattern, syntax, type, attr, config, accum = [], acceptable) { super(undefined, config, accum, acceptable); this.append(new syntax_1.SyntaxToken(syntax, pattern, 'table-syntax', config, accum, { 'Stage-1': ':', '!ExtToken': '', TranscludeToken: ':' }), // @ts-expect-error abstract class new attributes_1.AttributesToken(attr, 'table-attrs', type, config, accum)); /* NOT FOR BROWSER */ this.protectChildren([0, 1]); } escape() { LSP: { // eslint-disable-line no-unused-labels for (const child of this.childNodes) { if (child instanceof syntax_1.SyntaxToken) { escapeTable(child); } else { child.escape(); } } } } /* NOT FOR BROWSER */ cloneNode() { const [syntax, attr, ...cloned] = this.cloneChildNodes(); return debug_1.Shadow.run(() => { const C = this.constructor, token = new C(undefined, undefined, this.getAttribute('config')); token.firstChild.safeReplaceWith(syntax); token.childNodes[1].safeReplaceWith(attr); if (token.is('td')) { // TdToken token.childNodes[2].safeReplaceWith(cloned[0]); } else { token.safeAppend(cloned); } return token; }); } /** @private */ setSyntax(syntax, esc) { const { firstChild } = this; firstChild.replaceChildren(syntax); if (esc) { escapeTable(firstChild); } } } exports.TableBaseToken = TableBaseToken; constants_1.classes['TableBaseToken'] = __filename;