-
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #266 from lucperkins/heading-adapter
Add custom heading adapter
- Loading branch information
Showing
8 changed files
with
215 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
extern crate comrak; | ||
extern crate slug; | ||
|
||
use comrak::{ | ||
adapters::{HeadingAdapter, HeadingMeta}, | ||
markdown_to_html_with_plugins, ComrakOptions, ComrakPlugins, | ||
}; | ||
|
||
fn main() { | ||
let adapter = CustomHeadingAdapter; | ||
let options = ComrakOptions::default(); | ||
let mut plugins = ComrakPlugins::default(); | ||
plugins.render.heading_adapter = Some(&adapter); | ||
|
||
print_html( | ||
"Some text.\n\n## Please hide me from search\n\nSome other text", | ||
&options, | ||
&plugins, | ||
); | ||
print_html( | ||
"Some text.\n\n### Here is some `code`\n\nSome other text", | ||
&options, | ||
&plugins, | ||
); | ||
print_html( | ||
"Some text.\n\n### Here is some **bold** text and some *italicized* text\n\nSome other text", | ||
&options, | ||
&plugins | ||
); | ||
print_html("# Here is a [link](/)", &options, &plugins); | ||
} | ||
|
||
struct CustomHeadingAdapter; | ||
|
||
impl HeadingAdapter for CustomHeadingAdapter { | ||
fn enter(&self, heading: &HeadingMeta) -> String { | ||
let id = slug::slugify(&heading.content); | ||
|
||
let search_include = !&heading.content.contains("hide"); | ||
|
||
format!( | ||
"<h{} id=\"{}\" data-search-include=\"{}\">", | ||
heading.level, id, search_include | ||
) | ||
} | ||
|
||
fn exit(&self, heading: &HeadingMeta) -> String { | ||
format!("</h{}>", heading.level) | ||
} | ||
} | ||
|
||
fn print_html(document: &str, options: &ComrakOptions, plugins: &ComrakPlugins) { | ||
let html = markdown_to_html_with_plugins(document, options, plugins); | ||
println!("{}", html); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters