Skip to content

Fix webview font-family so it falls back to monospace for custom editor.fontFamily#326118

Open
Alexoswin wants to merge 1 commit into
microsoft:mainfrom
Alexoswin:fix/markdown-preview-monospace-fallback-324256
Open

Fix webview font-family so it falls back to monospace for custom editor.fontFamily#326118
Alexoswin wants to merge 1 commit into
microsoft:mainfrom
Alexoswin:fix/markdown-preview-monospace-fallback-324256

Conversation

@Alexoswin

Copy link
Copy Markdown

Problem

Reported in #324256: when editor.fontFamily is set to a custom font that either doesn't exist or doesn't itself include a generic monospace fallback, code blocks and inline code in the Markdown preview render in the browser's default font (often serif) instead of falling back to a monospace font.

Root cause

Every webview (including the Markdown preview) gets its --vscode-editor-font-family CSS variable from WebviewThemeDataProvider.getWebviewThemeData() in src/vs/workbench/contrib/webview/browser/themeing.ts. That code used the raw editor.fontFamily setting verbatim:

const editorFontFamily = configuration.fontFamily || EDITOR_FONT_DEFAULTS.fontFamily;

extensions/markdown-language-features/media/markdown.css consumes this variable with a var(--vscode-editor-font-family, <fallback list>), but that CSS fallback list only applies when the variable is unset — since themeing.ts always sets it to some string, the CSS-level fallback never kicks in.

The real Monaco text editor doesn't have this problem because BareFontInfo.getMassagedFontFamily() (src/vs/editor/common/config/fontInfo.ts) always appends the platform's default font family (which itself ends in the generic monospace keyword) as a fallback whenever the configured font differs from the default. themeing.ts never used this helper, which is the actual gap.

Fix

  • Extracted the fallback-appending logic out of BareFontInfo.getMassagedFontFamily() into a standalone exported function, applyFontFamilyFallback(), in fontInfo.ts (no behavior change for existing callers — getMassagedFontFamily() is now a thin wrapper around it).
  • themeing.ts now calls applyFontFamilyFallback() when computing the webview's editorFontFamily, so --vscode-editor-font-family always carries a monospace fallback chain, matching the real editor's behavior.

This fixes the Markdown preview specifically, and also fixes the same issue for any other webview that relies on --vscode-editor-font-family.

Testing performed

  • Added src/vs/editor/test/common/config/fontInfo.test.ts covering applyFontFamilyFallback(): custom font gets the fallback appended, a font with spaces gets quoted before the fallback is appended, an already-escaped font family isn't double-wrapped, and a font family equal to the platform default isn't duplicated.
  • tsc -p src/tsconfig.json --noEmit passes with no errors.
  • eslint passes on all changed files.
  • Manually verified the extracted fallback logic against the four test cases above with a standalone script producing the expected output on this platform's default ('Droid Sans Mono', monospace on Linux).

Fixes #324256

Webviews (e.g. the Markdown preview) receive editor.fontFamily verbatim
as the --vscode-editor-font-family CSS variable, with no fallback. If
the configured font is unavailable or omits a generic family, code
blocks render in the browser default (often serif) instead of
monospace, unlike the real editor which always appends a monospace
fallback via getMassagedFontFamily(). Extract that fallback logic into
an exported applyFontFamilyFallback() and reuse it when computing the
webview theme's font family.

Fixes microsoft#324256

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Aligns webview editor-font fallback behavior with Monaco, preventing serif rendering when custom fonts are unavailable.

Changes:

  • Extracts reusable font-family fallback logic.
  • Applies it to webview theme data.
  • Adds unit coverage for fallback behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/vs/editor/common/config/fontInfo.ts Extracts applyFontFamilyFallback.
src/vs/editor/test/common/config/fontInfo.test.ts Tests fallback and quoting behavior.
src/vs/workbench/contrib/webview/browser/themeing.ts Applies fallback to webview editor fonts.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Markdown preview does not fallback monospace font in code block if editor.fontFamily does not have monospace and font does not exist

3 participants