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>
This commit is contained in:
parent
cd5cf0740e
commit
425de3f865
|
@ -11,7 +11,7 @@ repository = "https://github.com/Xe/site"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
color-eyre = "0.5"
|
color-eyre = "0.5"
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
comrak = "0.12"
|
comrak = "0.12.1"
|
||||||
envy = "0.4"
|
envy = "0.4"
|
||||||
estimated_read_time = "1"
|
estimated_read_time = "1"
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
|
|
|
@ -36,7 +36,6 @@
|
||||||
|
|
||||||
.gruvbox-dark pre {
|
.gruvbox-dark pre {
|
||||||
background-color: #1d2021;
|
background-color: #1d2021;
|
||||||
padding: 0;
|
|
||||||
border: none
|
border: none
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -235,4 +234,4 @@
|
||||||
color: #3c3836;
|
color: #3c3836;
|
||||||
background-color: #bdae93;
|
background-color: #bdae93;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,8 +30,6 @@ fieldset {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
pre {
|
pre {
|
||||||
padding: 2rem;
|
|
||||||
margin: 1.75rem 0;
|
|
||||||
background-color: #fff;
|
background-color: #fff;
|
||||||
border: 1px solid #ccc;
|
border: 1px solid #ccc;
|
||||||
overflow: auto;
|
overflow: auto;
|
||||||
|
|
|
@ -37,3 +37,8 @@ img {
|
||||||
.conversation-chat {
|
.conversation-chat {
|
||||||
align-self: center;
|
align-self: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pre {
|
||||||
|
padding-left: 1em;
|
||||||
|
padding-right: 1em;
|
||||||
|
}
|
||||||
|
|
|
@ -1,10 +1,19 @@
|
||||||
use crate::templates::Html;
|
use crate::templates::Html;
|
||||||
use color_eyre::eyre::{Result, WrapErr};
|
use color_eyre::eyre::{Result, WrapErr};
|
||||||
use comrak::nodes::{Ast, AstNode, NodeValue};
|
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 std::cell::RefCell;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref SYNTECT_ADAPTER: SyntectAdapter<'static> = SyntectAdapter::new("base16-mocha.dark");
|
||||||
|
}
|
||||||
|
|
||||||
pub fn render(inp: &str) -> Result<String> {
|
pub fn render(inp: &str) -> Result<String> {
|
||||||
let mut options = ComrakOptions::default();
|
let mut options = ComrakOptions::default();
|
||||||
|
|
||||||
|
@ -20,6 +29,9 @@ pub fn render(inp: &str) -> Result<String> {
|
||||||
let arena = Arena::new();
|
let arena = Arena::new();
|
||||||
let root = parse_document(&arena, inp, &options);
|
let root = parse_document(&arena, inp, &options);
|
||||||
|
|
||||||
|
let mut plugins = ComrakPlugins::default();
|
||||||
|
plugins.render.codefence_syntax_highlighter = Some(&*SYNTECT_ADAPTER);
|
||||||
|
|
||||||
iter_nodes(root, &|node| {
|
iter_nodes(root, &|node| {
|
||||||
let mut data = node.data.borrow_mut();
|
let mut data = node.data.borrow_mut();
|
||||||
match &mut data.value {
|
match &mut data.value {
|
||||||
|
@ -33,10 +45,10 @@ pub fn render(inp: &str) -> Result<String> {
|
||||||
node.detach();
|
node.detach();
|
||||||
let mut message = vec![];
|
let mut message = vec![];
|
||||||
for child in node.children() {
|
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 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);
|
crop_letters(&mut message, 3);
|
||||||
message.drain((message.len() - 5)..);
|
message.drain((message.len() - 5)..);
|
||||||
let mood = without_first(u.path());
|
let mood = without_first(u.path());
|
||||||
|
@ -57,7 +69,7 @@ pub fn render(inp: &str) -> Result<String> {
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let mut html = vec![];
|
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")
|
String::from_utf8(html).wrap_err("post is somehow invalid UTF-8")
|
||||||
}
|
}
|
||||||
|
|
|
@ -147,6 +147,4 @@
|
||||||
@}
|
@}
|
||||||
@}
|
@}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script src="https://cdn.christine.website/file/christine-static/prism/prism.js"></script>
|
|
||||||
@:footer_html()
|
@:footer_html()
|
||||||
|
|
|
@ -59,7 +59,6 @@ la budza pu cusku lu
|
||||||
<link rel="stylesheet" href="/css/hack.css" />
|
<link rel="stylesheet" href="/css/hack.css" />
|
||||||
<link rel="stylesheet" href="/css/gruvbox-dark.css" />
|
<link rel="stylesheet" href="/css/gruvbox-dark.css" />
|
||||||
<link rel="stylesheet" href="/css/shim.css" />
|
<link rel="stylesheet" href="/css/shim.css" />
|
||||||
<link rel="stylesheet" href="https://cdn.christine.website/file/christine-static/prism/prism.css" />
|
|
||||||
@if Utc::now().month() == 12 || Utc::now().month() == 1 || Utc::now().month() == 2 { <link rel="stylesheet" href="/css/snow.css" /> }
|
@if Utc::now().month() == 12 || Utc::now().month() == 1 || Utc::now().month() == 2 { <link rel="stylesheet" href="/css/snow.css" /> }
|
||||||
<link rel="manifest" href="/static/manifest.json" />
|
<link rel="manifest" href="/static/manifest.json" />
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue