From 9a2ba8c330471dd9212604373f790f4bc0bddcae41dafbc8a89dc32560910939 Mon Sep 17 00:00:00 2001 From: evilchili Date: Fri, 16 Oct 2026 13:27:47 -0700 Subject: [PATCH] interface cleanup --- dist/tags/base.d.ts | 2 +- dist/tags/base.js | 30 ++---------------------------- dist/tags/inline.d.ts | 3 --- dist/tags/inline.js | 9 +++------ dist/types.d.ts | 16 +++------------- src/ts/tags/base.ts | 5 +++-- src/ts/tags/inline.ts | 9 +++------ src/ts/types.ts | 20 +++----------------- 8 files changed, 18 insertions(+), 76 deletions(-) diff --git a/dist/tags/base.d.ts b/dist/tags/base.d.ts index f446410..53144e9 100644 --- a/dist/tags/base.d.ts +++ b/dist/tags/base.d.ts @@ -7,6 +7,7 @@ export declare class Tag implements TagType { precedence: number; isBlock: boolean; showDelimitersInEditorNodes: boolean; + template: string; childNodesToMarkdown(element: HTMLElement, callback: Function): string; tokenize(role: TokenRole, sequence: string, source: string): Token | null; toHTML(token: Token, callback: Function): string; @@ -19,6 +20,5 @@ export declare class TagCollection implements TagCollectionType { ordered: TagType[]; map: Map; constructor(tags: TagType[]); - private validate; getByElementName(element: string): TagType | undefined; } diff --git a/dist/tags/base.js b/dist/tags/base.js index ede0a58..e673f0d 100644 --- a/dist/tags/base.js +++ b/dist/tags/base.js @@ -9,6 +9,7 @@ class Tag { precedence = 50; isBlock = false; showDelimitersInEditorNodes = true; + template = ''; childNodesToMarkdown(element, callback) { return Array.from(element.childNodes) .map(child => callback(child)) @@ -98,7 +99,6 @@ class TagCollection { tags = {}; ordered; map; - //openers: RegExp[]; constructor(tags) { for (const tag of tags) { if (tag.constructor.name in this.tags) { @@ -111,7 +111,6 @@ class TagCollection { const p2 = b.precedence - (b.isBlock ? 100 : 0); return p1 < p2 ? -1 : 1; }); - //this.validate(); this.map = new Map(); for (const tag of this.ordered) { for (const key of tag.opener) { @@ -122,35 +121,10 @@ class TagCollection { } } } - validate() { - const inline = this.ordered.filter(t => { return !t.opener; }); - for (let outer = 0; outer < inline.length; outer++) { - for (let inner = outer + 1; inner < inline.length; inner++) { - const first = this.ordered[outer]; - const second = this.ordered[inner]; - if (!(first.opener && second.opener)) { - continue; - } - const firstIsPrefix = second.opener[0].startsWith(first.opener[0]); - const secondIsPrefix = first.opener[0].startsWith(second.opener[0]); - if (!firstIsPrefix && !secondIsPrefix) { - continue; - } - const longer = first.opener.length > second.opener.length ? first : second; - const shorter = first.opener.length > second.opener.length ? second : first; - if (longer.precedence >= shorter.precedence) { - throw new Error(`Tag "${longer.constructor.name}" (opener "${longer.opener}") must have ` + - `lower precedence than "${shorter.constructor.name}" (opener "${shorter.opener}") ` + - `because its opener is a prefix match. ` + - `Got ${longer.constructor.name}=${longer.precedence}, ${shorter.constructor.name}=${shorter.precedence}.`); - } - } - } - } getByElementName(element) { const el = element.toUpperCase(); return this.ordered.filter(t => { - return [...t.element, ...t.aliases ?? []].includes(el); + return t.element.includes(el); })[0] || undefined; } } diff --git a/dist/tags/inline.d.ts b/dist/tags/inline.d.ts index bb87c04..9b35321 100644 --- a/dist/tags/inline.d.ts +++ b/dist/tags/inline.d.ts @@ -1,13 +1,11 @@ import { Tag } from "./base"; export declare class Bold extends Tag { element: string[]; - aliases: string[]; opener: string[]; precedence: number; } export declare class Italic extends Tag { element: string[]; - aliases: string[]; opener: string[]; precedence: number; } @@ -18,7 +16,6 @@ export declare class BoldItalic extends Tag { } export declare class Strikethrough extends Tag { element: string[]; - aliases: string[]; opener: string[]; } export declare class Code extends Tag { diff --git a/dist/tags/inline.js b/dist/tags/inline.js index 8d04c94..9d094ce 100644 --- a/dist/tags/inline.js +++ b/dist/tags/inline.js @@ -3,15 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.Code = exports.Strikethrough = exports.BoldItalic = exports.Italic = exports.Bold = void 0; const base_1 = require("./base"); class Bold extends base_1.Tag { - element = ['STRONG']; - aliases = ['B']; + element = ['STRONG', 'B']; opener = ['**', '__']; precedence = 40; } exports.Bold = Bold; class Italic extends base_1.Tag { - element = ['EM']; - aliases = ['I']; + element = ['EM', 'I']; opener = ['*', '_']; precedence = 30; } @@ -23,8 +21,7 @@ class BoldItalic extends base_1.Tag { } exports.BoldItalic = BoldItalic; class Strikethrough extends base_1.Tag { - element = ['DEL']; - aliases = ['S', 'STRIKE']; + element = ['DEL', 'S', 'STRIKE']; opener = ['~~']; } exports.Strikethrough = Strikethrough; diff --git a/dist/types.d.ts b/dist/types.d.ts index 80a77fd..84a99e2 100644 --- a/dist/types.d.ts +++ b/dist/types.d.ts @@ -7,27 +7,17 @@ export interface Token { groups?: { [key: string]: string; }; - /** For link tokens: the href and optional title. */ - href?: string; - title?: string; meta?: Record; } -/** - * A Tag is the core abstraction: it knows how to match markdown syntax, - * convert it to HTML, and convert the HTML back to markdown. Tags are - * registered by HTML selector (e.g. 'STRONG,B') so the converter can - * look them up during HTML→markdown conversion. - */ export interface TagType { element: string[]; opener: string[]; closer: string | null; precedence: number; isBlock: boolean; - template?: string; - pattern?: RegExp | null; - showDelimitersInEditorNodes?: boolean; - aliases?: string[]; + showDelimitersInEditorNodes: boolean; + template: string; + pattern: RegExp | null; tokenize(role: TokenRole, sequence: string, value: string): Token | null; toHTML: (token: Token, callback: Function) => string; toMarkdown: (element: HTMLElement, callback: Function) => string; diff --git a/src/ts/tags/base.ts b/src/ts/tags/base.ts index 8e10cb2..ff03842 100644 --- a/src/ts/tags/base.ts +++ b/src/ts/tags/base.ts @@ -9,6 +9,8 @@ export class Tag implements TagType { isBlock: boolean = false; showDelimitersInEditorNodes: boolean = true; + template = ''; + childNodesToMarkdown(element: HTMLElement, callback: Function): string { return Array.from(element.childNodes) .map(child => callback(child)) @@ -102,7 +104,6 @@ export class TagCollection implements TagCollectionType { tags: Record = {}; ordered: TagType[]; map: Map; - //openers: RegExp[]; constructor(tags: TagType[]) { for (const tag of tags) { @@ -131,7 +132,7 @@ export class TagCollection implements TagCollectionType { getByElementName(element: string): TagType | undefined { const el = element.toUpperCase(); return this.ordered.filter(t => { - return [...t.element, ...t.aliases ?? []].includes(el); + return t.element.includes(el); })[0] || undefined; } } diff --git a/src/ts/tags/inline.ts b/src/ts/tags/inline.ts index f02cf36..ce56ff5 100644 --- a/src/ts/tags/inline.ts +++ b/src/ts/tags/inline.ts @@ -1,15 +1,13 @@ import { Tag } from "./base"; export class Bold extends Tag { - element = ['STRONG']; - aliases = ['B']; + element = ['STRONG', 'B']; opener = ['**', '__']; precedence = 40; } export class Italic extends Tag { - element = ['EM']; - aliases = ['I']; + element = ['EM', 'I']; opener = ['*', '_']; precedence = 30; } @@ -21,8 +19,7 @@ export class BoldItalic extends Tag { } export class Strikethrough extends Tag { - element = ['DEL']; - aliases = ['S', 'STRIKE']; + element = ['DEL', 'S', 'STRIKE']; opener = ['~~']; } diff --git a/src/ts/types.ts b/src/ts/types.ts index 82d1044..c769f87 100644 --- a/src/ts/types.ts +++ b/src/ts/types.ts @@ -3,35 +3,21 @@ export type TokenRole = 'open' | 'close' | 'block'; export interface Token { role: TokenRole; value: string; - sequence?: string; tag?: TagType; - groups?: { [key: string]: string; }; - - /** For link tokens: the href and optional title. */ - href?: string; - title?: string; - meta?: Record; } -/** - * A Tag is the core abstraction: it knows how to match markdown syntax, - * convert it to HTML, and convert the HTML back to markdown. Tags are - * registered by HTML selector (e.g. 'STRONG,B') so the converter can - * look them up during HTML→markdown conversion. - */ export interface TagType { element: string[]; opener: string[]; closer: string | null; precedence: number; isBlock: boolean; - template?: string; - pattern?: RegExp | null; - showDelimitersInEditorNodes?: boolean; - aliases?: string[]; + showDelimitersInEditorNodes: boolean; + template: string; + pattern: RegExp | null; tokenize(role: TokenRole, sequence: string, value: string): Token | null; toHTML: (token: Token, callback: Function) => string;