1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2024-11-25 18:51:09 +00:00

Render markdown headings one level smaller (fixes #46)

This commit is contained in:
Felix Ableitner 2024-10-15 13:00:00 +02:00
parent 0b5b60fc9c
commit 222cecb668

View file

@ -1,6 +1,7 @@
use katex; use katex;
use markdown_it::{ use markdown_it::{
parser::inline::{InlineRule, InlineState}, parser::inline::{InlineRule, InlineState},
plugins::cmark::block::{heading::ATXHeading, lheading::SetextHeader},
MarkdownIt, MarkdownIt,
Node, Node,
NodeValue, NodeValue,
@ -10,7 +11,19 @@ use once_cell::sync::OnceCell;
pub fn render_markdown(text: &str) -> String { pub fn render_markdown(text: &str) -> String {
static INSTANCE: OnceCell<MarkdownIt> = OnceCell::new(); static INSTANCE: OnceCell<MarkdownIt> = OnceCell::new();
INSTANCE.get_or_init(markdown_parser).parse(text).render() let mut parsed = INSTANCE.get_or_init(markdown_parser).parse(text);
// Make markdown headings one level smaller, so that h1 becomes h2 etc, and markdown titles
// are smaller than page title.
parsed.walk_mut(|node, _| {
if let Some(heading) = node.cast_mut::<ATXHeading>() {
heading.level += 1;
}
if let Some(heading) = node.cast_mut::<SetextHeader>() {
heading.level += 1;
}
});
parsed.render()
} }
fn markdown_parser() -> MarkdownIt { fn markdown_parser() -> MarkdownIt {