-
Notifications
You must be signed in to change notification settings - Fork 191
fix(super-editor): use fontFamily key for markdown code block monospace font #3820
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
smxmdrissel
wants to merge
1
commit into
superdoc-dev:main
from
smxmdrissel:fix/markdown-code-block-monospace-font
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
packages/super-editor/src/editors/v1/core/helpers/markdown/constants.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /** | ||
| * Shared constants for the Markdown ↔ ProseMirror conversion pipeline. | ||
| */ | ||
|
|
||
| /** | ||
| * The font family used to mark monospace ("code") text in both directions of | ||
| * the markdown ↔ ProseMirror conversion: | ||
| * - `mdastToProseMirror.ts` applies this font (via a `textStyle` mark and/or | ||
| * direct `runProperties.fontFamily`) to fenced code blocks (`code`) and | ||
| * inline code spans (`inlineCode`). | ||
| * - `proseMirrorToMdast.ts` detects this font (on either the mark or the | ||
| * direct run properties) to convert monospace runs back into `inlineCode` | ||
| * mdast nodes. | ||
| * | ||
| * Keeping this as a single shared constant avoids the two directions | ||
| * silently drifting out of sync if the monospace font ever changes. | ||
| */ | ||
| export const MARKDOWN_MONOSPACE_FONT = 'Courier New'; |
163 changes: 163 additions & 0 deletions
163
packages/super-editor/src/editors/v1/core/helpers/markdown/mdastToProseMirror.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { Schema } from 'prosemirror-model'; | ||
| import { convertMdastToBlocks } from './mdastToProseMirror.js'; | ||
| import { MARKDOWN_MONOSPACE_FONT } from './constants.js'; | ||
| import type { MdastConversionContext } from './types.js'; | ||
| import type { Root, Code, Paragraph, InlineCode } from 'mdast'; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Minimal schema mirroring SuperEditor's node/mark types (enough for | ||
| // mdastToProseMirror's JSON output to round-trip through nodeFromJSON if | ||
| // a test wants to materialize it — most assertions here work directly on | ||
| // the raw JSON returned by convertMdastToBlocks). | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| const schema = new Schema({ | ||
| nodes: { | ||
| doc: { content: 'block+' }, | ||
| paragraph: { | ||
| group: 'block', | ||
| content: 'inline*', | ||
| attrs: { | ||
| paragraphProperties: { default: null }, | ||
| numberingProperties: { default: null }, | ||
| }, | ||
| }, | ||
| run: { | ||
| inline: true, | ||
| group: 'inline', | ||
| content: 'inline*', | ||
| attrs: { runProperties: { default: null } }, | ||
| }, | ||
| text: { group: 'inline' }, | ||
| lineBreak: { inline: true, group: 'inline' }, | ||
| }, | ||
| marks: { | ||
| bold: {}, | ||
| italic: {}, | ||
| strike: {}, | ||
| textStyle: { attrs: { fontFamily: { default: null } } }, | ||
| }, | ||
| }); | ||
|
|
||
| function createMockEditor(): any { | ||
| return { | ||
| converter: { numbering: { definitions: {}, abstracts: {} } }, | ||
| state: { doc: { descendants: () => {} } }, | ||
| }; | ||
| } | ||
|
|
||
| function makeCtx(): MdastConversionContext { | ||
| return { | ||
| editor: createMockEditor(), | ||
| schema, | ||
| diagnostics: [], | ||
| options: { dryRun: true }, | ||
| }; | ||
| } | ||
|
|
||
| /** Build a minimal mdast Root wrapping a single `code` node. */ | ||
| function codeRoot(value: string, lang: string | null = null): Root { | ||
| const code: Code = { type: 'code', lang, value }; | ||
| return { type: 'root', children: [code] }; | ||
| } | ||
|
|
||
| /** Build a minimal mdast Root wrapping a paragraph containing an inlineCode span. */ | ||
| function inlineCodeRoot(value: string): Root { | ||
| const inlineCode: InlineCode = { type: 'inlineCode', value }; | ||
| const paragraph: Paragraph = { type: 'paragraph', children: [inlineCode] }; | ||
| return { type: 'root', children: [paragraph] }; | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Tests | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe('convertMdastToBlocks — code block (fenced)', () => { | ||
| it('wraps a single-line code block in a paragraph of one run', () => { | ||
| const blocks = convertMdastToBlocks(codeRoot('const x = 1;'), makeCtx()); | ||
|
|
||
| expect(blocks).toHaveLength(1); | ||
| const [paragraph] = blocks; | ||
| expect(paragraph.type).toBe('paragraph'); | ||
| expect(paragraph.content).toHaveLength(1); | ||
|
|
||
| const [run] = paragraph.content!; | ||
| expect(run.type).toBe('run'); | ||
| expect(run.content).toHaveLength(1); | ||
| expect(run.content![0]).toMatchObject({ type: 'text', text: 'const x = 1;' }); | ||
| }); | ||
|
|
||
| it('sets both the textStyle mark and the direct runProperties.fontFamily attr', () => { | ||
| const blocks = convertMdastToBlocks(codeRoot('code'), makeCtx()); | ||
| const run = blocks[0].content![0]; | ||
|
|
||
| // Mark (consumed by calculateInlineRunPropertiesPlugin's mark-based recalculation) | ||
| expect(run.content![0].marks).toEqual([{ type: 'textStyle', attrs: { fontFamily: MARKDOWN_MONOSPACE_FONT } }]); | ||
|
|
||
| // Direct attrs (consumed by callers that never dispatch through a live Editor) | ||
| expect(run.attrs).toEqual({ | ||
| runProperties: { | ||
| fontFamily: { | ||
| ascii: MARKDOWN_MONOSPACE_FONT, | ||
| eastAsia: MARKDOWN_MONOSPACE_FONT, | ||
| hAnsi: MARKDOWN_MONOSPACE_FONT, | ||
| cs: MARKDOWN_MONOSPACE_FONT, | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('uses the OOXML rFonts key ("fontFamily"), never a bare "rFonts" key', () => { | ||
| const blocks = convertMdastToBlocks(codeRoot('code'), makeCtx()); | ||
| const run = blocks[0].content![0]; | ||
|
|
||
| expect(run.attrs!.runProperties).toHaveProperty('fontFamily'); | ||
| expect(run.attrs!.runProperties).not.toHaveProperty('rFonts'); | ||
| }); | ||
|
|
||
| it('joins multiple lines with lineBreak nodes, one run per non-empty line', () => { | ||
| const blocks = convertMdastToBlocks(codeRoot('line one\nline two\nline three'), makeCtx()); | ||
| const content = blocks[0].content!; | ||
|
|
||
| // run, lineBreak, run, lineBreak, run | ||
| expect(content.map((n) => n.type)).toEqual(['run', 'lineBreak', 'run', 'lineBreak', 'run']); | ||
| expect(content[0].content![0].text).toBe('line one'); | ||
| expect(content[2].content![0].text).toBe('line two'); | ||
| expect(content[4].content![0].text).toBe('line three'); | ||
| }); | ||
|
|
||
| it('emits a lineBreak but no run for a blank line within the code block', () => { | ||
| const blocks = convertMdastToBlocks(codeRoot('line one\n\nline three'), makeCtx()); | ||
| const content = blocks[0].content!; | ||
|
|
||
| // run, lineBreak, lineBreak, run — no run is emitted for the empty middle line | ||
| expect(content.map((n) => n.type)).toEqual(['run', 'lineBreak', 'lineBreak', 'run']); | ||
| }); | ||
|
|
||
| it('produces an empty paragraph for an empty code block', () => { | ||
| const blocks = convertMdastToBlocks(codeRoot(''), makeCtx()); | ||
|
|
||
| expect(blocks).toHaveLength(1); | ||
| expect(blocks[0].type).toBe('paragraph'); | ||
| expect(blocks[0].content).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('convertMdastToBlocks — inline code', () => { | ||
| it('wraps inline code in a run with only the textStyle mark (no direct runProperties)', () => { | ||
| const blocks = convertMdastToBlocks(inlineCodeRoot('inline'), makeCtx()); | ||
| const run = blocks[0].content![0]; | ||
|
|
||
| expect(run.type).toBe('run'); | ||
| expect(run.content![0]).toMatchObject({ | ||
| type: 'text', | ||
| text: 'inline', | ||
| marks: [{ type: 'textStyle', attrs: { fontFamily: MARKDOWN_MONOSPACE_FONT } }], | ||
| }); | ||
|
|
||
| // Unlike the fenced code-block case, inlineCode relies solely on the mark — | ||
| // see the comment on convertCodeBlock for why these two paths differ. | ||
| expect(run.attrs).toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Misses w:ascii monospace
🐞 Bug≡ CorrectnessAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools