diff --git a/core/modules/parsers/wikiparser/rules/codeblock.js b/core/modules/parsers/wikiparser/rules/codeblock.js index d5a7ca9ad..f6e9463f3 100644 --- a/core/modules/parsers/wikiparser/rules/codeblock.js +++ b/core/modules/parsers/wikiparser/rules/codeblock.js @@ -15,6 +15,9 @@ Wiki text rule for code blocks. For example: /** * @typedef {import("$:/core/modules/parsers/base.js").ParseTreeAttribute} ParseTreeAttribute + * @typedef {import('../wikirulebase.js').WikiRuleBase} WikiRuleBase + * @typedef {import('../../base.js').Parser} Parser + * @typedef {typeof exports & WikiRuleBase} ThisRule */ /** @@ -48,6 +51,7 @@ exports.init = function(parser) { /** * Parses the code block and returns an array of `codeblock` widgets. * + * @this {ThisRule} * @returns {ParseTreeCodeblockNode[]} An array containing a single codeblock widget object. */ exports.parse = function() { diff --git a/core/modules/parsers/wikiparser/rules/html.js b/core/modules/parsers/wikiparser/rules/html.js index 2c5d7bcee..b55351513 100644 --- a/core/modules/parsers/wikiparser/rules/html.js +++ b/core/modules/parsers/wikiparser/rules/html.js @@ -21,6 +21,9 @@ This is a widget invocation /** * @typedef {import("$:/core/modules/parsers/base.js").ParseTreeAttribute} ParseTreeAttribute + * @typedef {import('../wikirulebase.js').WikiRuleBase} WikiRuleBase + * @typedef {import('../../base.js').Parser} Parser + * @typedef {typeof exports & WikiRuleBase & {nextTag?:ParseTreeHtmlNode;}} ThisRule */ /** @@ -51,10 +54,18 @@ This is a widget invocation exports.name = "html"; exports.types = {inline: true, block: true}; +/** + * @param {Parser} parser + */ exports.init = function(parser) { this.parser = parser; }; +/** + * @this {ThisRule} + * @param {number} startPos + * @returns {number | undefined} Start position of next HTML tag + */ exports.findNextMatch = function(startPos) { // Find the next tag this.nextTag = this.findNextTag(this.parser.source,startPos,{ @@ -65,7 +76,7 @@ exports.findNextMatch = function(startPos) { /** * Parse most recent match - * + * @this {ThisRule} * @returns {ParseTreeHtmlNode[]} Array containing parsed HTML tag object */ exports.parse = function() { @@ -191,6 +202,15 @@ exports.parseTag = function(source,pos,options) { return node; }; +/** + * Find the next HTML tag in the source + * + * @this {ThisRule} + * @param {string} source + * @param {number} pos - Position to start searching from + * @param {Object} options + * @returns {Object|null} Parsed tag object or null if no valid tag is found + */ exports.findNextTag = function(source,pos,options) { // A regexp for finding candidate HTML tags var reLookahead = /<([a-zA-Z\-\$\.]+)/g; diff --git a/core/modules/parsers/wikiparser/wikirulebase.js b/core/modules/parsers/wikiparser/wikirulebase.js index 8aa960ef7..f2a319942 100644 --- a/core/modules/parsers/wikiparser/wikirulebase.js +++ b/core/modules/parsers/wikiparser/wikirulebase.js @@ -12,22 +12,46 @@ Base class for wiki parser rules /*global $tw: false */ "use strict"; -/* -This constructor is always overridden with a blank constructor, and so shouldn't be used -*/ -var WikiRuleBase = function() { +/** + * @typedef {import('../base').Parser} Parser + */ + +/** + * Base class for wiki rules. + * This constructor is always overridden with a blank constructor, and so shouldn't be used + * + * @class + * @constructor + */ +function WikiRuleBase() { + /** + * Inject by parser + * @type {Record<"pragma"|"block"|"inline", boolean>} + */ + this.is = {}; + /** + * @type {RegExp} + */ + this.matchRegExp; }; -/* -To be overridden by individual rules -*/ +/** + * Initialize rule with given parser instance + * To be overridden by individual rules + * + * @param {Parser} parser - Parser instance to initialize with + */ WikiRuleBase.prototype.init = function(parser) { this.parser = parser; }; -/* -Default implementation of findNextMatch uses RegExp matching -*/ +/** + * Default implementation of findNextMatch uses RegExp matching + * Find next match in source starting from given position using RegExp matching + * + * @param {number} startPos - Position to start searching from + * @returns {number|undefined} Index of next match or undefined if no match is found + */ WikiRuleBase.prototype.findNextMatch = function(startPos) { this.matchRegExp.lastIndex = startPos; this.match = this.matchRegExp.exec(this.parser.source); diff --git a/types/ast.d.ts b/types/ast.d.ts index f5d3bbe75..ca8fdc90b 100644 --- a/types/ast.d.ts +++ b/types/ast.d.ts @@ -1,6 +1,6 @@ -import { ParseTreeCodeblockNode } from './core/modules/parsers/wikiparser/rules/codeblock'; -export { ParseTreeCodeblockNode } from './core/modules/parsers/wikiparser/rules/codeblock'; -import { ParseTreeHtmlNode } from './core/modules/parsers/wikiparser/rules/html'; -export { ParseTreeHtmlNode } from './core/modules/parsers/wikiparser/rules/html'; +import { ParseTreeCodeblockNode } from '$:/core/modules/parsers/wikiparser/rules/codeblock.js'; +export { ParseTreeCodeblockNode } from '$:/core/modules/parsers/wikiparser/rules/codeblock.js'; +import { ParseTreeHtmlNode } from '$:/core/modules/parsers/wikiparser/rules/html.js'; +export { ParseTreeHtmlNode } from '$:/core/modules/parsers/wikiparser/rules/html.js'; export type WikiASTNode = ParseTreeCodeblockNode | ParseTreeHtmlNode;