From 87d63e4a1bd604a0cda6102cad9c59bbce8f792e Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Fri, 8 Sep 2023 15:37:37 +0300 Subject: [PATCH] feat(tools/cheddar): allow disabling tagfilter extension Makes it possible to do things like embedding YouTube videos in blog posts rendered through Cheddar. Change-Id: I6aed943c7bec0167b9f009d36dd067c52c6d3083 Reviewed-on: https://cl.tvl.fyi/c/depot/+/9275 Tested-by: BuildkiteCI Reviewed-by: Mark Shevchenko --- tools/cheddar/src/bin/cheddar.rs | 14 ++++++++++++-- tools/cheddar/src/lib.rs | 15 +++++++++++---- tools/cheddar/src/tests.rs | 2 +- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/tools/cheddar/src/bin/cheddar.rs b/tools/cheddar/src/bin/cheddar.rs index 48c504d53..73017a223 100644 --- a/tools/cheddar/src/bin/cheddar.rs +++ b/tools/cheddar/src/bin/cheddar.rs @@ -48,7 +48,7 @@ fn markdown_endpoint(request: &rouille::Request) -> rouille::Response { for text in texts.values_mut() { let mut buf: Vec = Vec::new(); - format_markdown(&mut text.as_bytes(), &mut buf); + format_markdown(&mut text.as_bytes(), &mut buf, true); *text = String::from_utf8_lossy(&buf).to_string(); } @@ -89,6 +89,12 @@ fn main() { .long("about-filter") .takes_value(false), ) + .arg( + Arg::with_name("no-tagfilter") + .help("Disable HTML tag filter") + .long("no-tagfilter") + .takes_value(false), + ) .arg( Arg::with_name("sourcegraph-server") .help("Run as a Sourcegraph compatible web-server") @@ -122,7 +128,11 @@ fn main() { let mut out_handle = stdout.lock(); if matches.is_present("about-filter") && filename.ends_with(".md") { - format_markdown(&mut in_handle, &mut out_handle); + format_markdown( + &mut in_handle, + &mut out_handle, + !matches.is_present("no-tagfilter"), + ); } else { format_code( &THEMES.themes["InspiredGitHub"], diff --git a/tools/cheddar/src/lib.rs b/tools/cheddar/src/lib.rs index c56789eca..be8bc7f82 100644 --- a/tools/cheddar/src/lib.rs +++ b/tools/cheddar/src/lib.rs @@ -233,6 +233,7 @@ fn format_callout_paragraph(callout: Callout) -> NodeValue { pub fn format_markdown_with_shortlinks( reader: &mut R, writer: &mut W, + tagfilter: bool, shortlinks: &[Shortlink], ) { let document = { @@ -244,7 +245,13 @@ pub fn format_markdown_with_shortlinks( }; let arena = Arena::new(); - let root = parse_document(&arena, &linkify_shortlinks(document, shortlinks), &MD_OPTS); + + let mut opts = MD_OPTS.clone(); + if !tagfilter { + opts.extension.tagfilter = false; + } + + let root = parse_document(&arena, &linkify_shortlinks(document, shortlinks), &opts); // This node must exist with a lifetime greater than that of the parsed AST // in case that callouts are encountered (otherwise insertion into the tree @@ -280,11 +287,11 @@ pub fn format_markdown_with_shortlinks( } }); - format_html(root, &MD_OPTS, writer).expect("Markdown rendering failed"); + format_html(root, &opts, writer).expect("Markdown rendering failed"); } -pub fn format_markdown(reader: &mut R, writer: &mut W) { - format_markdown_with_shortlinks(reader, writer, &TVL_LINKS) +pub fn format_markdown(reader: &mut R, writer: &mut W, tagfilter: bool) { + format_markdown_with_shortlinks(reader, writer, tagfilter, &TVL_LINKS) } fn find_syntax_for_file(filename: &str) -> &'static SyntaxReference { diff --git a/tools/cheddar/src/tests.rs b/tools/cheddar/src/tests.rs index 4d22a1cf0..0550acd35 100644 --- a/tools/cheddar/src/tests.rs +++ b/tools/cheddar/src/tests.rs @@ -6,7 +6,7 @@ use std::io::BufReader; fn expect_markdown(input: &str, expected: &str) { let mut input_buf = BufReader::new(input.trim().as_bytes()); let mut out_buf: Vec = vec![]; - format_markdown(&mut input_buf, &mut out_buf); + format_markdown(&mut input_buf, &mut out_buf, true); let out_string = String::from_utf8(out_buf).expect("output should be UTF8"); assert_eq!(out_string.trim(), expected.trim());