1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-01-05 23:10:28 +00:00

feat: refine the ast type

This commit is contained in:
linonetwo 2024-09-12 17:49:18 +08:00
parent c56afea741
commit 06204b1580
4 changed files with 63 additions and 15 deletions

View File

@ -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() {

View File

@ -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;

View File

@ -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);

8
types/ast.d.ts vendored
View File

@ -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;