diff options
author | Eric Huss <eric@huss.org> | 2020-11-10 14:02:01 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-10 14:02:01 -0800 |
commit | 643d5ecc5c080061ae19da4374044561a484aef3 (patch) | |
tree | 771d9d2981d65adbb74aa285bba042aeea9a3003 /src/book | |
parent | eaa69142059c04ca14aa23ffeb1fe1469663403f (diff) | |
parent | 9e9cf49c503a9d7a47ec57457d0d5875f100b987 (diff) |
Merge pull request #1285 from FrankHB/patch-1
Handled UTF-8 BOM
Diffstat (limited to 'src/book')
-rw-r--r-- | src/book/book.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/book/book.rs b/src/book/book.rs index 071d1589..35f66438 100644 --- a/src/book/book.rs +++ b/src/book/book.rs @@ -264,6 +264,10 @@ fn load_chapter<P: AsRef<Path>>( format!("Unable to read \"{}\" ({})", link.name, location.display()) })?; + if content.as_bytes().starts_with(b"\xef\xbb\xbf") { + content.replace_range(..3, ""); + } + let stripped = location .strip_prefix(&src_dir) .expect("Chapters are always inside a book"); @@ -394,6 +398,29 @@ And here is some \ } #[test] + fn load_a_single_chapter_with_utf8_bom_from_disk() { + let temp_dir = TempFileBuilder::new().prefix("book").tempdir().unwrap(); + + let chapter_path = temp_dir.path().join("chapter_1.md"); + File::create(&chapter_path) + .unwrap() + .write_all(("\u{feff}".to_owned() + DUMMY_SRC).as_bytes()) + .unwrap(); + + let link = Link::new("Chapter 1", chapter_path); + + let should_be = Chapter::new( + "Chapter 1", + DUMMY_SRC.to_string(), + "chapter_1.md", + Vec::new(), + ); + + let got = load_chapter(&link, temp_dir.path(), Vec::new()).unwrap(); + assert_eq!(got, should_be); + } + + #[test] fn cant_load_a_nonexistent_chapter() { let link = Link::new("Chapter 1", "/foo/bar/baz.md"); |