From 602e5eacd5e787ff230162717da1baa71b57060b Mon Sep 17 00:00:00 2001 From: lin onetwo Date: Tue, 20 Aug 2024 21:23:13 +0800 Subject: [PATCH] feat: add example --- .../parsers/wikiparser/rules/codeblock.js | 34 ++++++++++++++++++- core/modules/parsers/wikiparser/try.js | 15 ++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 core/modules/parsers/wikiparser/try.js diff --git a/core/modules/parsers/wikiparser/rules/codeblock.js b/core/modules/parsers/wikiparser/rules/codeblock.js index 6c3480566..3dddba894 100644 --- a/core/modules/parsers/wikiparser/rules/codeblock.js +++ b/core/modules/parsers/wikiparser/rules/codeblock.js @@ -1,4 +1,5 @@ -/*\ +// @ts-check +/** title: $:/core/modules/parsers/wikiparser/rules/codeblock.js type: application/javascript module-type: wikirule @@ -11,7 +12,28 @@ Wiki text rule for code blocks. For example: ``` ``` +@module $:/core/modules/parsers/wikiparser/rules/codeblock.js + \*/ + +/** + * Represents the `codeblock` rule. + * + * @typedef {Object} CodeblockNode + * @property {string} type - The type of the widget, which is "codeblock". + * @property {Object} attributes - The attributes of the codeblock. + * @property {Object} attributes.code - The code attribute object. + * @property {string} attributes.code.type - The type of the code attribute, which is "string". + * @property {string} attributes.code.value - The actual code content within the code block. + * @property {number} attributes.code.start - The start position of the code in the source text. + * @property {number} attributes.code.end - The end position of the code in the source text. + * @property {Object} attributes.language - The language attribute object. + * @property {string} attributes.language.type - The type of the language attribute, which is "string". + * @property {string} attributes.language.value - The language specified after the triple backticks, if any. + * @property {number} attributes.language.start - The start position of the language string in the source text. + * @property {number} attributes.language.end - The end position of the language string in the source text. + */ + (function(){ /*jslint node: true, browser: true */ @@ -21,12 +43,22 @@ Wiki text rule for code blocks. For example: exports.name = "codeblock"; exports.types = {block: true}; +/** + * Initializes the codeblock rule with the given parser. + * + * @param {Object} parser - The parser object that manages the state of the parsing process. + */ exports.init = function(parser) { this.parser = parser; // Regexp to match and get language if defined this.matchRegExp = /```([\w-]*)\r?\n/mg; }; +/** + * Parses the code block and returns an array of `codeblock` widgets. + * + * @returns {CodeblockNode[]} An array containing a single codeblock widget object. + */ exports.parse = function() { var reEnd = /(\r?\n```$)/mg; var languageStart = this.parser.pos + 3, diff --git a/core/modules/parsers/wikiparser/try.js b/core/modules/parsers/wikiparser/try.js new file mode 100644 index 000000000..f04bfe296 --- /dev/null +++ b/core/modules/parsers/wikiparser/try.js @@ -0,0 +1,15 @@ +// @ts-check +/** + * @typedef {import('./rules/codeblock').CodeblockNode} CodeblockNode + */ + +/** + * A function that processes a code block. + * + * @param {CodeblockNode[]} codeblocks - An array of codeblock rules. + */ +function processCodeblocks(codeblocks) { + codeblocks.forEach(function(cb) { + console.log(cb.attributes.code.value); + }); +}