mirror of
https://github.com/Nutomic/ibis.git
synced 2024-11-22 07:21:10 +00:00
Render markdown headings one level smaller (fixes #46)
This commit is contained in:
parent
0b5b60fc9c
commit
222cecb668
1 changed files with 14 additions and 1 deletions
|
@ -1,6 +1,7 @@
|
|||
use katex;
|
||||
use markdown_it::{
|
||||
parser::inline::{InlineRule, InlineState},
|
||||
plugins::cmark::block::{heading::ATXHeading, lheading::SetextHeader},
|
||||
MarkdownIt,
|
||||
Node,
|
||||
NodeValue,
|
||||
|
@ -10,7 +11,19 @@ use once_cell::sync::OnceCell;
|
|||
|
||||
pub fn render_markdown(text: &str) -> String {
|
||||
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 {
|
||||
|
|
Loading…
Reference in a new issue