From 5edcc2f14e1f106167d0820a68260c7d300f50ed Mon Sep 17 00:00:00 2001 From: Jovyn Tan <61113575+jovyntls@users.noreply.github.com> Date: Tue, 21 Mar 2023 09:46:16 +0800 Subject: [PATCH] Refactor core package to use Node types (#2221) Refactor core package to use Node types In #2201, we introduced a unifying type for nodes. Most of the core package is not yet using the new types. Let's refactor the core package to utilise these new types, and add a utility parseHTML method that avoids further `as unknown as` typecasts throughout the codebase. Doing so allows us to make full use of TypeScript's type-checking abilities while avoiding code duplication. For more information on the rationale of the unifying types, refer to #2201. --- packages/core/src/html/FootnoteProcessor.ts | 16 +++--- packages/core/src/html/MdAttributeRenderer.ts | 37 ++++++------- packages/core/src/html/NodeProcessor.ts | 22 ++++---- packages/core/src/html/SiteLinkManager.ts | 14 ++--- packages/core/src/html/codeblockProcessor.ts | 12 ++-- packages/core/src/html/headerProcessor.ts | 14 ++--- packages/core/src/html/linkProcessor.ts | 55 +++++++++---------- .../core/src/html/siteAndPageNavProcessor.ts | 39 +++++-------- .../core/src/html/vueSlotSyntaxProcessor.ts | 20 +++---- packages/core/src/html/warnings.ts | 14 ++--- packages/core/src/plugins/Plugin.ts | 10 ++-- packages/core/src/plugins/PluginManager.ts | 6 +- packages/core/src/utils/node.ts | 9 +++ .../core/src/variables/VariableProcessor.ts | 10 ++-- .../core/test/unit/html/NodeProcessor.test.ts | 13 +++-- .../test/unit/html/SiteLinkManager.test.ts | 15 +++-- .../core/test/unit/html/linkProcessor.test.ts | 42 +++++++------- 17 files changed, 163 insertions(+), 185 deletions(-) diff --git a/packages/core/src/html/FootnoteProcessor.ts b/packages/core/src/html/FootnoteProcessor.ts index 09dc6b9663..8fded4a3d6 100644 --- a/packages/core/src/html/FootnoteProcessor.ts +++ b/packages/core/src/html/FootnoteProcessor.ts @@ -1,5 +1,5 @@ import cheerio from 'cheerio'; -import { DomElement } from 'htmlparser2'; +import { MbNode, parseHTML } from '../utils/node'; import { MARKBIND_FOOTNOTE_POPOVER_ID_PREFIX } from './constants'; /* @@ -13,7 +13,7 @@ export class FootnoteProcessor { this.renderedFootnotes = []; } - processMbTempFootnotes(node: DomElement) { + processMbTempFootnotes(node: MbNode) { const $ = cheerio(node); const content = $.html(); if (content) { @@ -22,7 +22,7 @@ export class FootnoteProcessor { $.remove(); } - combineFootnotes(processNode: (nd: DomElement) => any): string { + combineFootnotes(processNode: (nd: MbNode) => any): string { let hasFootnote = false; const prefix = '