1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-01-20 22:16:52 +00:00

refactor: move some type to base class

This commit is contained in:
linonetwo 2024-09-12 13:17:03 +08:00
parent cdceeddddb
commit 8f0d37e24c
3 changed files with 49 additions and 10 deletions

View File

@ -0,0 +1,42 @@
/**
* Base structure for a parse node
*
* @typedef {Object} ParseTreeNode
* @property {string} type - Type of widget
* @property {Object} [attributes] - Attributes of widget
* @property {ParseTreeNode[]} [children] - Array of child parse nodes
*/
/**
* Base class for parsers. This only provides typing
*
* @class
* @param {string} type - Content type of text to be parsed
* @param {string} text - Text to be parsed
* @param {Object} options - Parser options
* @param {boolean} [options.parseAsInline=false] - If true, text will be parsed as an inline run
* @param {Object} options.wiki - Reference to wiki store in use
* @param {string} [options._canonical_uri] - Optional URI of content if text is missing or empty
* @param {boolean} [options.configTrimWhiteSpace=false] - If true, parser trims white space
*/
function Parser(type, text, options) {
/**
* Result AST
* @type {ParseTreeNode[]}
*/
this.tree = [];
/**
* Original text without modifications
* @type {string}
*/
this.source = text;
/**
* Source content type in MIME format
* @type {string}
*/
this.type = type;
}
exports.Parser = Parser;

View File

@ -25,18 +25,16 @@ Attributes are stored as hashmaps of the following objects:
/*global $tw: false */
"use strict";
/**
* @typedef {import('../base').Parser} Parser
*/
/**
* WikiParser class for parsing text of a specified MIME type.
*
* @class
* @extends {Parser}
* @constructor
* @param {string} type - Content type of the text to be parsed
* @param {string} text - Text to be parsed
* @param {Object} options - Parser options
* @param {boolean} [options.parseAsInline=false] - If true, the text will be parsed as an inline run
* @param {Object} options.wiki - Reference to the wiki store in use
* @param {string} [options._canonical_uri] - Optional URI of the content if text is missing or empty
* @param {boolean} [options.configTrimWhiteSpace=false] - If true, the parser trims white space
*/
function WikiParser(type,text,options) {
this.wiki = options.wiki;

View File

@ -1037,7 +1037,7 @@ exports.initParsers = function(moduleType) {
};
/**
* @typedef {import('$:/core/modules/parsers/wikiparser/wikiparser.js')["text/vnd.tiddlywiki"]} WikiParser
* @typedef {import('$:/core/modules/parsers/base.js').Parser} Parser
*/
/**
@ -1051,13 +1051,12 @@ exports.initParsers = function(moduleType) {
* @param {string} [options.defaultType="text/vnd.tiddlywiki"] - Default type to use if no parser is found for specified type
* @param {boolean} [options.configTrimWhiteSpace=false] - If true, trims white space according to configuration
*
* @returns {WikiParser|null} Parser instance or null if no parser is found
* @returns {Parser|null} Parser instance or null if no parser is found
*/
exports.parseText = function(type,text,options) {
text = text || "";
options = options || {};
/**
* @type WikiParser
* Select a parser
*/
var Parser = $tw.Wiki.parsers[type];