diff options
| author | Asherah Connor <1915+kivikakk@users.noreply.github.com> | 2021-09-09 07:00:11 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-08 17:00:11 -0400 |
| commit | 425de3f8650e56632e4d91a948676717250f3ccd (patch) | |
| tree | 7a43180b11c261510052c72c917591aea2902ed0 /src | |
| parent | cd5cf0740ee56b169abf26a19f979107c45b23b3 (diff) | |
| download | xesite-425de3f8650e56632e4d91a948676717250f3ccd.tar.xz xesite-425de3f8650e56632e4d91a948676717250f3ccd.zip | |
markdown: use comrak syntect (#393)
* markdown: use comrak syntect
* css: make the code samples look better
It ain't perfect, but it's probably good enough to start with:
https://media.discordapp.net/attachments/188796211543801856/885244826180808754/20210908_15h26m30s_grim.png
Signed-off-by: Christine Dodrill <me@christine.website>
Co-authored-by: Christine Dodrill <me@christine.website>
Diffstat (limited to 'src')
| -rw-r--r-- | src/app/markdown.rs | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/src/app/markdown.rs b/src/app/markdown.rs index dbc5ecf..6b37b67 100644 --- a/src/app/markdown.rs +++ b/src/app/markdown.rs @@ -1,10 +1,19 @@ use crate::templates::Html; use color_eyre::eyre::{Result, WrapErr}; use comrak::nodes::{Ast, AstNode, NodeValue}; -use comrak::{format_html, markdown_to_html, parse_document, Arena, ComrakOptions}; +use comrak::plugins::syntect::SyntectAdapter; +use comrak::{ + format_html_with_plugins, markdown_to_html_with_plugins, parse_document, Arena, ComrakOptions, + ComrakPlugins, +}; +use lazy_static::lazy_static; use std::cell::RefCell; use url::Url; +lazy_static! { + static ref SYNTECT_ADAPTER: SyntectAdapter<'static> = SyntectAdapter::new("base16-mocha.dark"); +} + pub fn render(inp: &str) -> Result<String> { let mut options = ComrakOptions::default(); @@ -20,6 +29,9 @@ pub fn render(inp: &str) -> Result<String> { let arena = Arena::new(); let root = parse_document(&arena, inp, &options); + let mut plugins = ComrakPlugins::default(); + plugins.render.codefence_syntax_highlighter = Some(&*SYNTECT_ADAPTER); + iter_nodes(root, &|node| { let mut data = node.data.borrow_mut(); match &mut data.value { @@ -33,10 +45,10 @@ pub fn render(inp: &str) -> Result<String> { node.detach(); let mut message = vec![]; for child in node.children() { - format_html(child, &options, &mut message)?; + format_html_with_plugins(child, &options, &mut message, &plugins)?; } let message = std::str::from_utf8(&message)?; - let mut message = markdown_to_html(message, &options); + let mut message = markdown_to_html_with_plugins(message, &options, &plugins); crop_letters(&mut message, 3); message.drain((message.len() - 5)..); let mood = without_first(u.path()); @@ -57,7 +69,7 @@ pub fn render(inp: &str) -> Result<String> { })?; let mut html = vec![]; - format_html(root, &options, &mut html).unwrap(); + format_html_with_plugins(root, &options, &mut html, &plugins).unwrap(); String::from_utf8(html).wrap_err("post is somehow invalid UTF-8") } |
