From 7821e00bc6c2dc7d27b701e4a5733b0a6e9369b4 Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Mon, 10 May 2021 17:36:33 +0100 Subject: [PATCH] Revert to internal option --- package.json | 1 - src/HtmlUtils.tsx | 8 +++++--- src/editor/serialize.ts | 15 ++++++++++----- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index a03f02871e..bd011ea6e2 100644 --- a/package.json +++ b/package.json @@ -75,7 +75,6 @@ "glob-to-regexp": "^0.4.1", "highlight.js": "^10.5.0", "html-entities": "^1.4.0", - "htmlparser2": "^6.1.0", "is-ip": "^3.1.0", "katex": "^0.12.0", "linkifyjs": "^2.1.9", diff --git a/src/HtmlUtils.tsx b/src/HtmlUtils.tsx index f2c2652280..ef5ac383e3 100644 --- a/src/HtmlUtils.tsx +++ b/src/HtmlUtils.tsx @@ -31,7 +31,6 @@ import katex from 'katex'; import { AllHtmlEntities } from 'html-entities'; import SettingsStore from './settings/SettingsStore'; import cheerio from 'cheerio'; -import * as htmlparser2 from 'htmlparser2'; import {tryTransformPermalinkToLocalHref} from "./utils/permalinks/Permalinks"; import {SHORTCODE_TO_EMOJI, getEmojiFromUnicode} from "./emoji"; @@ -423,9 +422,12 @@ export function bodyToHtml(content: IContent, highlights: string[], opts: IOpts safeBody = sanitizeHtml(formattedBody, sanitizeParams); if (SettingsStore.getValue("feature_latex_maths")) { - const phtml = cheerio.load(htmlparser2.parseDocument(safeBody, { + const phtml = cheerio.load(safeBody, { + // @ts-ignore: The `_useHtmlParser2` internal option is the + // simplest way to both parse and render using `htmlparser2`. + _useHtmlParser2: true, decodeEntities: false, - })); + }); // @ts-ignore - The types for `replaceWith` wrongly expect // Cheerio instance to be returned. phtml('div, span[data-mx-maths!=""]').replaceWith(function(i, e) { diff --git a/src/editor/serialize.ts b/src/editor/serialize.ts index 5db53064d5..cfdb36c7a8 100644 --- a/src/editor/serialize.ts +++ b/src/editor/serialize.ts @@ -22,7 +22,6 @@ import { AllHtmlEntities } from 'html-entities'; import SettingsStore from '../settings/SettingsStore'; import SdkConfig from '../SdkConfig'; import cheerio from 'cheerio'; -import * as htmlparser2 from 'htmlparser2'; export function mdSerialize(model: EditorModel) { return model.parts.reduce((html, part) => { @@ -117,16 +116,22 @@ export function htmlSerializeIfNeeded(model: EditorModel, {forceHTML = false} = const parser = new Markdown(md); if (!parser.isPlainText() || forceHTML) { // feed Markdown output to HTML parser - const phtml = cheerio.load(htmlparser2.parseDocument(parser.toHTML(), { + const phtml = cheerio.load(parser.toHTML(), { + // @ts-ignore: The `_useHtmlParser2` internal option is the + // simplest way to both parse and render using `htmlparser2`. + _useHtmlParser2: true, decodeEntities: false, - })); + }); if (SettingsStore.getValue("feature_latex_maths")) { // original Markdown without LaTeX replacements const parserOrig = new Markdown(orig); - const phtmlOrig = cheerio.load(htmlparser2.parseDocument(parserOrig.toHTML(), { + const phtmlOrig = cheerio.load(parserOrig.toHTML(), { + // @ts-ignore: The `_useHtmlParser2` internal option is the + // simplest way to both parse and render using `htmlparser2`. + _useHtmlParser2: true, decodeEntities: false, - })); + }); // since maths delimiters are handled before Markdown, // code blocks could contain mangled content.