wikiparser-node
Version:
A Node.js parser for MediaWiki markup with AST
176 lines (175 loc) • 5.16 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmbeddedCSSDocument = exports.EmbeddedJSONDocument = exports.stylelint = exports.htmlData = exports.cssLSP = exports.jsonLSP = exports.jsonTags = exports.loadMathJax = void 0;
const path_1 = __importDefault(require("path"));
const common_1 = require("@bhsd/common");
/* NOT FOR BROWSER */
const constants_1 = require("../util/constants");
let MathJax;
/**
* Load MathJax
* @param id MathJax module ID
*/
const loadMathJax = (id = 'mathjax') => {
try {
const jax = require(id);
MathJax ??= jax.init({
loader: {
load: ['input/tex', '[tex]/mhchem'],
},
tex: {
packages: { '[+]': ['mhchem'] },
/** @ignore */
formatError(_, error) {
throw error;
},
},
startup: { typeset: false },
});
return MathJax;
}
catch {
/* istanbul ignore next */
return undefined;
}
};
exports.loadMathJax = loadMathJax;
exports.jsonTags = ['templatedata', 'mapframe', 'maplink'];
exports.jsonLSP = (() => {
try {
const lsp = require('vscode-json-languageservice')
.getLanguageService({
/** @implements */
async schemaRequestService(uri) {
return (await fetch(uri)).text();
},
});
lsp.configure({
schemas: exports.jsonTags.map((tag) => {
const uri = path_1.default.join('..', '..', 'data', 'ext', tag);
try {
const schema = require(uri);
return {
uri,
fileMatch: [tag],
schema,
};
}
catch {
/* istanbul ignore next */
return false;
}
}).filter(Boolean),
});
return lsp;
}
catch {
/* istanbul ignore next */
return undefined;
}
})();
exports.cssLSP = (() => {
try {
return require('vscode-css-languageservice')
.getCSSLanguageService();
}
catch {
/* istanbul ignore next */
return undefined;
}
})();
exports.htmlData = (() => {
try {
return require('vscode-html-languageservice')
.getDefaultHTMLDataProvider();
}
catch {
/* istanbul ignore next */
return undefined;
}
})();
exports.stylelint = (async () => {
try {
return (await import('stylelint')).default;
}
catch {
/* istanbul ignore next */
return undefined;
}
})();
/** embedded document */
class EmbeddedDocument {
uri = '';
version = 0;
#root;
#content;
#offset;
#post;
/**
* @param id language ID
* @param root root token
* @param token current token
* @param pre padding before the content
* @param post padding after the content
*/
constructor(id, root, token, pre = '', post = '') {
this.languageId = id;
this.lineCount = root.getLines().length;
this.#root = root;
this.#content = token.toString();
this.#offset = token.getAbsoluteIndex();
this.pre = pre;
this.#post = post;
}
/** 原始文本 */
getContent() {
return this.#content;
}
/** @implements */
getText() {
return this.pre + this.getContent() + this.#post;
}
/** @implements */
positionAt(offset) {
const { top, left } = this.#root.posFromIndex(this.#offset + Math.max(Math.min(offset - this.pre.length, this.#content.length), 0));
return { line: top, character: left };
}
/** @implements */
offsetAt({ line, character }) {
return Math.min(Math.max(this.#root.indexFromPos(line, character) - this.#offset, 0), this.#content.length)
+ this.pre.length;
}
}
/** embedded JSON document */
class EmbeddedJSONDocument extends EmbeddedDocument {
/**
* @param root root token
* @param token current token
*/
constructor(root, token) {
super('json', root, token);
this.uri = token.name;
this.jsonDoc = exports.jsonLSP.parseJSONDocument(this);
}
}
exports.EmbeddedJSONDocument = EmbeddedJSONDocument;
/** embedded CSS document */
class EmbeddedCSSDocument extends EmbeddedDocument {
/**
* @param root root token
* @param token current token
*/
constructor(root, token) {
const { type, tag } = token.parentNode;
super('css', root, token, `${type === 'ext-attr' ? 'div' : tag}{`, '}');
this.styleSheet = exports.cssLSP.parseStylesheet(this);
}
getContent() {
return (0, common_1.sanitizeInlineStyle)(super.getContent());
}
}
exports.EmbeddedCSSDocument = EmbeddedCSSDocument;
constants_1.classes['EmbeddedCSSDocument'] = __filename;