Skip to content

Commit 643d5ec

Browse files
authored
Merge pull request #1285 from FrankHB/patch-1
Handled UTF-8 BOM
2 parents eaa6914 + 9e9cf49 commit 643d5ec

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/book/book.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ fn load_chapter<P: AsRef<Path>>(
264264
format!("Unable to read \"{}\" ({})", link.name, location.display())
265265
})?;
266266

267+
if content.as_bytes().starts_with(b"\xef\xbb\xbf") {
268+
content.replace_range(..3, "");
269+
}
270+
267271
let stripped = location
268272
.strip_prefix(&src_dir)
269273
.expect("Chapters are always inside a book");
@@ -393,6 +397,29 @@ And here is some \
393397
assert_eq!(got, should_be);
394398
}
395399

400+
#[test]
401+
fn load_a_single_chapter_with_utf8_bom_from_disk() {
402+
let temp_dir = TempFileBuilder::new().prefix("book").tempdir().unwrap();
403+
404+
let chapter_path = temp_dir.path().join("chapter_1.md");
405+
File::create(&chapter_path)
406+
.unwrap()
407+
.write_all(("\u{feff}".to_owned() + DUMMY_SRC).as_bytes())
408+
.unwrap();
409+
410+
let link = Link::new("Chapter 1", chapter_path);
411+
412+
let should_be = Chapter::new(
413+
"Chapter 1",
414+
DUMMY_SRC.to_string(),
415+
"chapter_1.md",
416+
Vec::new(),
417+
);
418+
419+
let got = load_chapter(&link, temp_dir.path(), Vec::new()).unwrap();
420+
assert_eq!(got, should_be);
421+
}
422+
396423
#[test]
397424
fn cant_load_a_nonexistent_chapter() {
398425
let link = Link::new("Chapter 1", "/foo/bar/baz.md");

0 commit comments

Comments
 (0)