Skip to content

Only apply markdown decorations when the note has markdown enabled - #3408

Open
gunjanjaswal wants to merge 5 commits into
Automattic:trunkfrom
gunjanjaswal:fix/3379-markdown-decorations-gate
Open

gunjanjaswal wants to merge 5 commits into
Automattic:trunkfrom
gunjanjaswal:fix/3379-markdown-decorations-gate

Conversation

@gunjanjaswal

Copy link
Copy Markdown

Fixes #3379.

Markdown decorations (headings, bold, italic, list markers, and so on) were applied in the editor even when the note had Markdown disabled. Every other Markdown behavior in note-content-editor.tsx gates on the note's markdown system tag, but the decoration path in setDecorators did not, so a plain note with a line like # comment or *.conf still got heading/emphasis styling. This is the v2.26.0 regression reported in #3379.

Changes proposed

  • markdown-decorations.ts: getMarkdownDecorations now takes a markdownEnabled flag (defaults to true) and returns [] when it is false.
  • note-content-editor.tsx: pass note.systemTags.includes('markdown'). The existing clear() stays unconditional, so decorations are removed when Markdown is toggled off on an open note.
  • markdown-decorations.test.ts: add a case asserting no decorations are returned when markdown is disabled.

Testing instructions

npx jest lib/markdown-decorations.test.ts

Or manually: open a note with Markdown disabled and a body containing # heading or **bold**, confirm the text stays plain, then toggle Markdown on and confirm the styling appears.

@gunjanjaswal

Copy link
Copy Markdown
Author

Gentle bump on this one. It only applies the markdown decorations when the note actually contains markdown, so plain notes stop getting decorated. It's been sitting a while and is still green and mergeable. Happy to rebase if it's drifted from trunk, just flagging it's ready for a look whenever someone has a moment.

@gunjanjaswal

Copy link
Copy Markdown
Author

Friendly bump on this. It's been open a while without a first look. The change is small and self-contained: only apply the markdown decorations when the note actually has markdown enabled, so plain notes stop getting them. Glad to rebase or adjust anything if that makes review easier.

@lotard

lotard commented Sep 26, 2026

Copy link
Copy Markdown

I support this, would be great to have normal plain text formatting back when no markdown is selected

@gunjanjaswal

Copy link
Copy Markdown
Author

Gentle bump now that @lotard has chimed in above wanting the same thing. It's a small, self-contained change: markdown decorations only get applied when the note actually has markdown turned on, so plain notes stop getting decorated. Still green and mergeable, and I'm glad to rebase if it's drifted from trunk. Would really appreciate a first look whenever someone has a moment.

@stephdau stephdau left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up, and sorry for the long wait! The change fixes the main case in #3379: a plain-text note opens with no Markdown styling. 👍

One thing needs fixing before we merge. Turning Markdown on or off for an open note in edit mode doesn't update the styling until the next edit. setDecorators only runs on mount, on content changes, on composition end and on search-query changes. When systemTags changes, componentDidUpdate only warms the renderer, and the editor is keyed by note ID, so it doesn't remount either. In practice:

  • Open a Markdown note containing # heading, then turn Markdown off: the heading styling stays until you type.
  • Turn Markdown on for a plain note: the text stays plain until you type.

(Turning it off from preview mode works, because the editor mounts fresh.) We reproduced this locally on the branch. The fix should be small. In the existing branch in componentDidUpdate:

if (
  prevProps.noteId !== this.props.noteId ||
  prevProps.note.systemTags !== this.props.note.systemTags
) {
  this.warmMarkdownRendererIfNeeded();
  this.setDecorators();
}

Two smaller suggestions:

  • Consider making markdownEnabled required instead of defaulting it to true. Today a caller that leaves it out silently gets decorations again, which is how #3379 happened, and the type checker wouldn't catch it.
  • A test on the editor side would help. The new test covers the helper, but nothing checks that NoteContentEditor passes the note's tag or updates when the tag changes. That's where this regression lives. Even a small test that switches systemTags on a mounted editor would cover the bug above.

Thanks again!

Addresses the review on Automattic#3408. Toggling markdown for a note open in edit mode
changes systemTags but neither remounts the editor (it is keyed by note id)
nor edits content, so setDecorators never re-ran and the styling stayed stale
until the next keystroke. Re-run it from componentDidUpdate when noteId or
systemTags changes.

Also make markdownEnabled a required argument to getMarkdownDecorations so a
caller that omits it can't silently fall back to decorating (the path that let
Automattic#3379 through), and add an editor-side test that drives componentDidUpdate
across a systemTags toggle to cover the regression directly.
@gunjanjaswal

Copy link
Copy Markdown
Author

Thanks for the thorough review, and for reproducing it on the branch, that pinned it exactly.

Fixed all three:

  • Stale styling on toggle. componentDidUpdate now calls setDecorators() alongside the renderer warm-up when noteId or systemTags changes, so turning markdown on or off on an open note re-decorates right away instead of waiting for the next keystroke. Confirmed both directions locally (heading styling clears when you turn markdown off, plain text picks it up when you turn it on).
  • Silent default. markdownEnabled is now a required argument to getMarkdownDecorations, so a caller that forgets it gets a type error rather than decorations sneaking back, which is how Markdown formatting applied even when markdown disabled #3379 slipped in. Updated the existing call sites to pass it explicitly.
  • Editor-side test. Added note-content-editor.test.tsx that drives componentDidUpdate across a systemTags toggle and asserts decorations get recomputed, plus a no-op case when nothing relevant changed. It exercises the handler directly rather than standing up Monaco, so it stays fast and deterministic. To reach it I exported the unconnected NoteContentEditor class.

Green locally: getMarkdownDecorations suite still passes with the required arg, and the three new editor tests pass.

@stephdau stephdau left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brilliant, @gunjanjaswal; thanks for the quick turnaround! This works as expected now: turning Markdown on or off for an open note updates the styling right away, in both directions, and plain-text notes stay plain. I tested it locally on the branch. Making markdownEnabled required and adding the editor-side test are both nice improvements. 🙌

Two optional notes, fine to skip:

  • The new editor test mocks setDecorators, so it covers the componentDidUpdate wiring, but not that setDecorators passes the note's tag to getMarkdownDecorations.
  • virtual: true in the monaco-editor mock isn't needed, since the package is installed.

Approving. Thanks again for fixing #3379!

Its package entry isn't resolvable by jest, so the mock registration throws
'Cannot find module' without it. Comment so the next reader doesn't remove it.
@gunjanjaswal

Copy link
Copy Markdown
Author

Thanks! On your two notes: the first is fair — the test proves the wiring but stubs setDecorators, so it doesn't assert the tag reaches getMarkdownDecorations. I kept it at the wiring level on purpose, since asserting the tag flows through means running the real setDecorators against a live Monaco model, which pulls the editor into the test. Happy to add that deeper pass in a follow-up if you'd like it.

On the second: virtual: true is actually load-bearing here — without it the mock registration throws Cannot find module 'monaco-editor', because jest can't resolve the package's entry even though it's installed. The branch you tested already had it, which is why it passed. I added a comment on that line so it doesn't get removed by mistake.

@gunjanjaswal

Copy link
Copy Markdown
Author

Gentle nudge on this one, @stephdau — thanks again for the review and approval. Is there anything else needed from me before it can land, or is it just waiting on a committer to merge? Happy to rebase if it's drifted since.

@gunjanjaswal

Copy link
Copy Markdown
Author

Another gentle nudge, @stephdau. This one's been approved and green since mid-October. Happy to rebase if it's drifted at all, just let me know if anything's blocking the merge. No rush, only keeping it on the radar.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Markdown formatting applied even when markdown disabled

3 participants