Skip to content

fix: set view blob charset=utf-8 to avoid non-ASCII mojibake - #10

Open
wang1212 wants to merge 1 commit into
portdeveloper:mainfrom
wang1212:fix/view-blob-charset-utf-8
Open

fix: set view blob charset=utf-8 to avoid non-ASCII mojibake#10
wang1212 wants to merge 1 commit into
portdeveloper:mainfrom
wang1212:fix/view-blob-charset-utf-8

Conversation

@wang1212

Copy link
Copy Markdown

Problem

When users click the View as Markdown action, the page opens in a new tab with non-ASCII text (e.g. CJK content) rendered as mojibake.

Root cause

viewAsMarkdown() opens the page content in a new tab via:

const blob = new Blob([contentToView], { type: "text/plain" });
const url = URL.createObjectURL(blob);
window.open(url, "_blank");

Blob encodes the JS string as UTF-8 bytes, but type: "text/plain" does not declare a charset. Per the HTTP/text/* default, the browser falls back to ISO-8859-1 (latin1) when decoding the resulting blob URL, so UTF-8 byte streams are decoded as single-byte latin1 characters — that's the mojibake.

Fix

Declare charset=utf-8 on the blob type:

-      const blob = new Blob([contentToView], { type: "text/plain" });
+      const blob = new Blob([contentToView], { type: "text/plain;charset=utf-8" });

The content is already UTF-8 (it's produced by the existing markdown extraction from the page DOM and from any generated per-page .md files), so declaring the charset only fixes decoding and changes nothing else.

Impact

view action only. copy and the AI-tool actions are unaffected (clipboard write and link construction already handle UTF-8 correctly).

Tested on

  • A Docusaurus 3.10 site with i18n.defaultLocale: zh-Hans, Chinese documentation pages.
  • Before: view tab shows 应用安全 etc.
  • After: view tab shows 应用安全 correctly.

/cc maintainers — happy to adjust if you prefer a different approach (e.g. serve via a generated .md route when generateMarkdownRoutes is enabled).

viewAsMarkdown opens page content in a new tab via Blob([content], { type: "text/plain" }).
Without an explicit charset, the browser falls back to ISO-8859-1 for text/plain,
so UTF-8 byte streams are decoded as latin1 and non-ASCII text (e.g. CJK) renders as
mojibake. Declare charset=utf-8 so the blob is decoded correctly.
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.

1 participant