feat: render markdown files in gists

master
Aravinth Manivannan 1 year ago
parent 8f2a8ed1a1
commit 31f2c6960a
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88

@ -107,7 +107,11 @@ impl GenerateHTML for FileInfo {
fn generate(&mut self) {
fn highlight(code: &mut String, filepath: &str) {
let q = SourcegraphQuery { code, filepath };
*code = q.syntax_highlight();
if filepath.ends_with("md") {
*code = q.render_markdown();
} else {
*code = q.syntax_highlight();
}
}
fn extract(f: &mut FileInfo) {

@ -16,6 +16,7 @@
*/
use std::path::Path;
use pulldown_cmark::{html, Options, Parser};
use syntect::highlighting::{Color, ThemeSet};
use syntect::html::highlighted_html_for_string;
use syntect::parsing::{SyntaxReference, SyntaxSet};
@ -40,6 +41,19 @@ pub struct SourcegraphQuery<'a> {
}
impl<'a> SourcegraphQuery<'a> {
pub fn render_markdown(&self) -> String {
// Set up options and parser. Strikethroughs are not part of the CommonMark standard
// and we therefore must enable it explicitly.
let options = Options::all();
// options.insert(Options::ENABLE_STRIKETHROUGH);
let parser = Parser::new_ext(self.code, options);
// Write to String buffer.
let mut html_output = String::new();
html::push_html(&mut html_output, parser);
html_output
}
pub fn syntax_highlight(&self) -> String {
// let ss = SYNTAX_SET;
let ts = ThemeSet::load_defaults();
@ -159,6 +173,20 @@ mod tests {
let _result = query.syntax_highlight();
}
#[test]
// renders markdown file into HTML
fn markdown_render_works() {
const README: &str = include_str!("./../../../../README.md");
let query = SourcegraphQuery {
filepath: "README.md",
code: README,
};
let result = query.render_markdown();
// NOTE: this test will fail if README.md doesn't contain a H1 and an input field
// of type checkbox that isn't checked/marked completed.
assert!(result.contains("<h1>"));
}
//#[test]
//fn cls_apex() {
// let syntax_set = SyntaxSet::load_defaults_newlines();

Loading…
Cancel
Save