fix: handle UTF-16 LE/BE encoded files with BOM#929
Open
lawrence3699 wants to merge 1 commit intocharmbracelet:masterfrom
Open
fix: handle UTF-16 LE/BE encoded files with BOM#929lawrence3699 wants to merge 1 commit intocharmbracelet:masterfrom
lawrence3699 wants to merge 1 commit intocharmbracelet:masterfrom
Conversation
Detect UTF-16 LE/BE byte order marks and convert file content to UTF-8 before rendering. Previously, UTF-16 encoded markdown files appeared mostly blank because the raw UTF-16 bytes were passed directly to the markdown renderer. The conversion uses golang.org/x/text (already a dependency) and falls back to the original bytes when no BOM is detected or if decoding fails. UTF-8 BOMs are also stripped. Fixes charmbracelet#513
There was a problem hiding this comment.
Pull request overview
This PR fixes rendering of UTF-16 LE/BE markdown files (with BOM) by converting file bytes to UTF-8 before passing content to the markdown renderer, addressing issue #513.
Changes:
- Add
utils.ToUTF8to detect BOMs (UTF-8/UTF-16 LE/BE) and normalize content to UTF-8. - Apply UTF-8 normalization across CLI, TUI file view, and stash local file loading paths.
- Add unit tests covering UTF-8 passthrough/BOM stripping and UTF-16 LE/BE decoding.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| utils/utils.go | Introduces ToUTF8 BOM detection + UTF-16→UTF-8 decoding utility. |
| utils/utils_test.go | Adds test coverage for UTF-8/UTF-16 BOM handling and edge cases (nil/empty, multibyte chars). |
| ui/ui.go | Normalizes on-disk file bytes via utils.ToUTF8 before frontmatter stripping/rendering. |
| ui/stash.go | Normalizes locally loaded stash file bytes via utils.ToUTF8 before storing into md.Body. |
| main.go | Normalizes CLI input bytes via utils.ToUTF8 prior to frontmatter stripping/rendering. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #513
Problem
UTF-16 LE/BE encoded markdown files (common when files are saved by Windows editors or converted from other formats) render as mostly blank content because the raw UTF-16 bytes are passed directly to the markdown renderer.
Fix
Detect UTF-16 byte order marks and convert file content to UTF-8 before rendering. The conversion is applied in all three file-reading paths:
executeCLI)ui.Init)loadLocalMarkdown)A
ToUTF8utility function handles BOM detection and conversion:FF FE): decoded viagolang.org/x/text/encoding/unicodeFE FF): decoded viagolang.org/x/text/encoding/unicodeEF BB BF): strippedgolang.org/x/textis already a dependency (v0.32.0), so no new dependencies are introduced.Before
After
Validation
go test ./...— all pass (7 new tests forToUTF8)go vet ./...— clean