|
| 1 | +use comrak::{ |
| 2 | + nodes::{AstNode, NodeValue}, |
| 3 | + Arena, ComrakOptions, |
| 4 | +}; |
| 5 | +use kl_hyphenate::{Hyphenator, Language, Load, Standard}; |
| 6 | +use std::error::Error; |
| 7 | + |
| 8 | +const SOFT_HYPHEN: char = '\u{00AD}'; |
| 9 | +const HYPHENATION_DICTIONARY: &str = "hyphenation-en-us.bincode"; |
| 10 | + |
| 11 | +pub(crate) fn render(input: &str) -> Result<String, Box<dyn Error>> { |
| 12 | + let options = ComrakOptions { |
| 13 | + ext_header_ids: Some(String::new()), |
| 14 | + unsafe_: true, // Allow rendering of raw HTML |
| 15 | + ..ComrakOptions::default() |
| 16 | + }; |
| 17 | + |
| 18 | + let hyphenator = Standard::from_path(Language::EnglishUS, HYPHENATION_DICTIONARY)?; |
| 19 | + |
| 20 | + let arena = Arena::new(); |
| 21 | + let ast = comrak::parse_document(&arena, input, &options); |
| 22 | + |
| 23 | + hyphenate(&ast, &hyphenator); |
| 24 | + |
| 25 | + let mut output = Vec::new(); |
| 26 | + comrak::format_html(&ast, &options, &mut output)?; |
| 27 | + Ok(String::from_utf8(output)?) |
| 28 | +} |
| 29 | + |
| 30 | +// Pre-compute points inside words where browsers can add hyphens during rendering. |
| 31 | +// |
| 32 | +// Support for the CSS rule `hyphens: auto`, which tells the browser to split words by adding |
| 33 | +// hyphens when there is no space left on the line, is quite low across browsers, preventing us |
| 34 | +// from using it on the blog. |
| 35 | +// |
| 36 | +// A widely supported alternative is the `hyphens: manual` rule, which moves the burden of deciding |
| 37 | +// *where* to break the word to the website. To properly use that rule, the website has to insert |
| 38 | +// the "soft hyphen" unicode character (U+00AD) in every position the browser is allowed to break |
| 39 | +// the word. |
| 40 | +// |
| 41 | +// The following piece of code walks through the Markdown AST adding those characters in every |
| 42 | +// suitable place, thanks to the kl-hyphenate library. |
| 43 | + |
| 44 | +fn hyphenate<'a>(node: &'a AstNode<'a>, hyphenator: &Standard) { |
| 45 | + match &mut node.data.borrow_mut().value { |
| 46 | + NodeValue::Text(content) => { |
| 47 | + if let Ok(string) = std::str::from_utf8(&content) { |
| 48 | + let hyphenated = add_soft_hyphens(string, hyphenator); |
| 49 | + *content = hyphenated.as_bytes().to_vec(); |
| 50 | + } |
| 51 | + } |
| 52 | + _ => {} |
| 53 | + } |
| 54 | + for child in node.children() { |
| 55 | + hyphenate(child, hyphenator); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +fn add_soft_hyphens(content: &str, hyphenator: &Standard) -> String { |
| 60 | + let mut output = String::with_capacity(content.len()); |
| 61 | + for (i, word) in content.split(' ').enumerate() { |
| 62 | + if i != 0 { |
| 63 | + output.push(' '); |
| 64 | + } |
| 65 | + let hyphenated = hyphenator.hyphenate(word); |
| 66 | + for (j, segment) in hyphenated.into_iter().segments().enumerate() { |
| 67 | + if j != 0 { |
| 68 | + output.push(SOFT_HYPHEN); |
| 69 | + } |
| 70 | + output.push_str(&segment); |
| 71 | + } |
| 72 | + } |
| 73 | + output |
| 74 | +} |
0 commit comments