From 3e45fd898ae84c4d0272307add686d7d2533e07f Mon Sep 17 00:00:00 2001 From: lucaronin Date: Thu, 16 Apr 2026 21:13:57 +0200 Subject: [PATCH] fix: limit markdown list highlighting to markers --- src/extensions/markdownHighlight.test.ts | 40 ++++++++++++++++++++++++ src/extensions/markdownHighlight.ts | 1 - 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/extensions/markdownHighlight.test.ts b/src/extensions/markdownHighlight.test.ts index c7f9349d..408903ee 100644 --- a/src/extensions/markdownHighlight.test.ts +++ b/src/extensions/markdownHighlight.test.ts @@ -29,6 +29,24 @@ function nodeNamesAt(view: EditorView, doc: string, needle: string) { return names } +function findLines(parent: HTMLDivElement) { + return Array.from(parent.querySelectorAll('.cm-line')) +} + +function expectMarkerOnlyHighlight( + line: HTMLDivElement | undefined, + expectedText: string, + expectedMarker: string, + expectedTrailingText: string, +) { + expect(line).toBeDefined() + expect(line!.textContent).toBe(expectedText) + expect(Array.from(line!.querySelectorAll('span'), (span) => span.textContent)).toEqual([expectedMarker]) + expect(line!.lastChild).not.toBeNull() + expect(line!.lastChild!.nodeType).toBe(Node.TEXT_NODE) + expect(line!.lastChild!.textContent).toBe(expectedTrailingText) +} + describe('markdownLanguage', () => { it('returns a valid extension', () => { const ext = markdownLanguage() @@ -89,4 +107,26 @@ describe('markdownLanguage', () => { view.destroy() parent.remove() }) + + it('styles only list markers while leaving list item text as plain content', async () => { + const doc = [ + '- item one', + ' - nested item', + '1. ordered item', + ].join('\n') + const { view, parent } = createView(doc) + + forceParsing(view, view.state.doc.length) + await new Promise((resolve) => setTimeout(resolve, 0)) + + const lines = findLines(parent) + expect(lines).toHaveLength(3) + + expectMarkerOnlyHighlight(lines[0], '- item one', '-', ' item one') + expectMarkerOnlyHighlight(lines[1], ' - nested item', '-', ' nested item') + expectMarkerOnlyHighlight(lines[2], '1. ordered item', '1.', ' ordered item') + + view.destroy() + parent.remove() + }) }) diff --git a/src/extensions/markdownHighlight.ts b/src/extensions/markdownHighlight.ts index bd096c55..a1da8d5e 100644 --- a/src/extensions/markdownHighlight.ts +++ b/src/extensions/markdownHighlight.ts @@ -17,7 +17,6 @@ const markdownHighlightStyle = HighlightStyle.define([ { tag: tags.link, color: '#0969da', textDecoration: 'underline' }, { tag: tags.url, color: '#0969da' }, { tag: tags.monospace, color: '#c9383e', backgroundColor: 'rgba(175,184,193,0.15)', borderRadius: '3px' }, - { tag: tags.list, color: '#c9383e' }, { tag: tags.quote, color: '#636c76', fontStyle: 'italic' }, { tag: tags.separator, color: '#636c76' }, { tag: tags.processingInstruction, color: '#c9383e', fontWeight: '600' },