Open platform to collect and prioritize feedback
fix(ui): show < in code as typed instead of <
Matt Roberts6cb1ef19aa46
diff --git a/public/services/markdown.spec.ts b/public/services/markdown.spec.tsindex b87d2c01d..d773423e0 100644--- a/public/services/markdown.spec.ts+++ b/public/services/markdown.spec.ts@@ -210,3 +210,38 @@ describe("mentions", () => {expect(spans.map((e) => e.textContent)).toEqual(["@Jane Doe"])})})++describe("text is shown exactly as typed", () => {+ // Raw HTML used to be escaped before parsing, so code then escaped it a second time and+ // `List<String>` displayed as `List<String>`.+ const shown = (html: string) => {+ const container = document.createElement("div")+ container.innerHTML = html+ return container.textContent+ }++ const cases: [string, string][] = [+ ["```\n<b>x</b> List<String> a && b\n```", "<b>x</b> List<String> a && b\n"],+ ["`List<String>` and `a && b`", "List<String> and a && b"],+ ["`<b>` shows the entity itself", "<b> shows the entity itself"],+ ["Hello <b>Beautiful</b> World & more", "Hello <b>Beautiful</b> World & more"],+ ["x < y > z", "x < y > z"],+ ]++ cases.forEach(([input, expected]) => {+ test(`full: ${input}`, () => {+ expect(shown(markdown.full(input))).toEqual(expected)+ })+ })++ test("plainText and toText keep code as typed", () => {+ expect(shown(markdown.plainText("`List<String>` and `a && b`"))).toEqual("List<String> and a && b")+ expect(markdown.toText("`List<String>` and `a && b`")).toEqual("List<String> and a && b")+ })++ test("angle-bracket autolinks become links, as in the editor", () => {+ expect(markdown.full("<https://example.com>")).toEqual(+ '<p><a class="text-link" href="https://example.com" rel="noopener nofollow" target="_blank">https://example.com</a></p>'+ )+ })+})diff --git a/public/services/markdown.ts b/public/services/markdown.tsindex 80a37dae6..502da22c6 100644--- a/public/services/markdown.ts+++ b/public/services/markdown.ts@@ -31,8 +31,7 @@ if (DOMPurify.isSupported) {// override that writes token text/attributes into HTML must escape it itself.// Text and attribute values. marked 5+ escapes text (including ' -> '); we keep apostrophes-// literal to match the historical output, while preserving existing entities (input has already-// been through encodeHTML, so "<" arrives as "<") and escaping & < > ".+// literal to match the historical output, while preserving existing entities and escaping & < > ".const escapeText = (s: string): string =>s.replace(/&(?![#\w]+;)/g, "&")@@ -70,9 +69,19 @@ const fullRenderer: RendererObject = {const rendered = "tokens" in token && token.tokens ? this.parser.parseInline(token.tokens) : escapeText(token.text)return renderMentions(rendered)},+ html({ text }) {+ // Not reached (raw HTML isn't tokenized, see noRawHTML), but never emit it unescaped.+ return escapeText(text)+ },}++// Raw HTML is never rendered: with its tokenizers off, "<b>" is just text and is escaped once+// by the text/code renderers. (Escaping "<" before parsing instead made code escape it twice,+// showing `List<String>` as `List<String>`, and broke <https://...> autolinks.)+const noRawHTML = { tokenizer: { html: () => undefined, tag: () => undefined } }+const markedFull = new Marked({ gfm: true, breaks: true })-markedFull.use({ renderer: fullRenderer })+markedFull.use(noRawHTML, { renderer: fullRenderer })// Plain-text renderer: strip all formatting down to readable text.const plainTextRenderer: RendererObject = {@@ -114,9 +123,8 @@ const plainTextRenderer: RendererObject = {},}const markedPlainText = new Marked({ gfm: true, breaks: true })-markedPlainText.use({ renderer: plainTextRenderer })+markedPlainText.use(noRawHTML, { renderer: plainTextRenderer })-const encodeHTML = (s: string) => s.replace(/</g, "<")const stripTags = (input: string) => input.replace(/<[^>]*>/g, "")const sanitize = (input: string) => (DOMPurify.isSupported ? DOMPurify.sanitize(input) : stripTags(input))// Helper function to decode HTML entities back to readable characters@@ -135,12 +143,12 @@ const decodeHtmlEntities = (text: string): string => {}export const full = (input: string): string => {- return sanitize((markedFull.parse(encodeHTML(input)) as string).trim())+ return sanitize((markedFull.parse(input) as string).trim())}// HTML with the formatting stripped, safe to insert as HTML (<Markdown style="plainText" />)export const plainText = (input: string): string => {- return sanitize((markedPlainText.parse(encodeHTML(input)) as string).trim())+ return sanitize((markedPlainText.parse(input) as string).trim())}// Plain text with entities decoded (e.g. auto-generated titles). Never insert the result as HTML