From 8af35399b9eedc6ead545fc7bc80fe5d29ea9c77 Mon Sep 17 00:00:00 2001 From: Leandro Pereira Date: Wed, 19 Jun 2024 13:14:45 -0400 Subject: [PATCH] Add `:features` option `:syntax_highlight_inline_style` (#43) * Add `:features` option `:syntax_highlight_inline_style` --- CHANGELOG.md | 1 + lib/mdex.ex | 3 ++- lib/mdex/types/options.ex | 3 ++- native/comrak_nif/src/inkjet_adapter.rs | 24 ++++++++++++++++++++---- native/comrak_nif/src/lib.rs | 10 ++++++++-- native/comrak_nif/src/types/options.rs | 1 + test/mdex_test.exs | 15 +++++++++++++++ 7 files changed, 49 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee07640..9f696b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * Bump ammonia to 4.0 * Add new `extension` options: multiline_block_quotes, math_dollars, math_code, shortcodes, wikilinks_title_after_pipe, wikilinks_title_before_pipe * Add new `render` option: escaped_char_spans + * Add new option `features.syntax_highlight_inline_style` to control whether to embed inline styles or not. Default is `true`. ## 0.1.16 (2024-04-29) diff --git a/lib/mdex.ex b/lib/mdex.ex index 249f2c9..78e9e4b 100644 --- a/lib/mdex.ex +++ b/lib/mdex.ex @@ -41,7 +41,8 @@ defmodule MDEx do * `:sanitize` (default `false`) - sanitize output using [ammonia](https://crates.io/crates/ammonia).\n Recommended if passing `render: [unsafe_: true]` * `:syntax_highlight_theme` (default `"onedark"`) - syntax highlight code fences using [autumn themes](https://github.com/leandrocp/autumn/tree/main/priv/themes), - you should pass the filename without special chars and without extension, for example you should pass `syntax_highlight_theme: "adwaita_dark"` to use the [Adwaita Dark](https://github.com/leandrocp/autumn/blob/main/priv/themes/adwaita-dark.toml) theme. + you should pass the filename without special chars and without extension, for example you should pass `syntax_highlight_theme: "adwaita_dark"` to use the [Adwaita Dark](https://github.com/leandrocp/autumn/blob/main/priv/themes/adwaita-dark.toml) theme + * `:syntax_highlight_inline_style` (default `true`) - embed styles in the output for each generated token. You'll need to [serve CSS themes](https://github.com/leandrocp/autumn?tab=readme-ov-file#linked) if inline styles are disabled to properly highlight code ## Examples diff --git a/lib/mdex/types/options.ex b/lib/mdex/types/options.ex index e080a32..aa85650 100644 --- a/lib/mdex/types/options.ex +++ b/lib/mdex/types/options.ex @@ -42,7 +42,8 @@ end defmodule MDEx.Types.FeaturesOptions do @moduledoc false defstruct sanitize: false, - syntax_highlight_theme: "onedark" + syntax_highlight_theme: "onedark", + syntax_highlight_inline_style: true end defmodule MDEx.Types.Options do diff --git a/native/comrak_nif/src/inkjet_adapter.rs b/native/comrak_nif/src/inkjet_adapter.rs index 46ebb87..66386ab 100644 --- a/native/comrak_nif/src/inkjet_adapter.rs +++ b/native/comrak_nif/src/inkjet_adapter.rs @@ -9,16 +9,31 @@ use tree_sitter_highlight::Highlighter; #[derive(Debug)] pub struct InkjetAdapter<'a> { theme: &'a Theme, + inline_style: bool, +} + +impl<'a> Default for InkjetAdapter<'a> { + fn default() -> Self { + let default_theme = themes::theme("onedark").unwrap(); + + InkjetAdapter { + theme: default_theme, + inline_style: true, + } + } } impl<'a> InkjetAdapter<'a> { - pub fn new(theme: &'a str) -> Self { + pub fn new(theme: &'a str, inline_style: bool) -> Self { let theme = match themes::theme(theme) { Some(theme) => theme, None => themes::theme("onedark").unwrap(), }; - Self { theme } + Self { + theme, + inline_style, + } } } @@ -48,7 +63,8 @@ impl<'a> SyntaxHighlighterAdapter for InkjetAdapter<'a> { for event in highlights { let event = event.expect("expected a highlight event"); - let inner_highlights = autumn::inner_highlights(source, event, self.theme, true); + let inner_highlights = + autumn::inner_highlights(source, event, self.theme, self.inline_style); write!(output, "{}", inner_highlights)? } @@ -60,7 +76,7 @@ impl<'a> SyntaxHighlighterAdapter for InkjetAdapter<'a> { output: &mut dyn Write, _attributes: HashMap, ) -> io::Result<()> { - let pre_tag = autumn::open_pre_tag(self.theme, None, true); + let pre_tag = autumn::open_pre_tag(self.theme, None, self.inline_style); write!(output, "{}", pre_tag) } diff --git a/native/comrak_nif/src/lib.rs b/native/comrak_nif/src/lib.rs index d765310..eb5623e 100644 --- a/native/comrak_nif/src/lib.rs +++ b/native/comrak_nif/src/lib.rs @@ -17,7 +17,7 @@ rustler::init!("Elixir.MDEx.Native", [to_html, to_html_with_options]); #[rustler::nif(schedule = "DirtyCpu")] fn to_html(md: &str) -> String { - let inkjet_adapter = InkjetAdapter::new("onedark"); + let inkjet_adapter = InkjetAdapter::default(); let mut plugins = ComrakPlugins::default(); plugins.render.codefence_syntax_highlighter = Some(&inkjet_adapter); markdown_to_html_with_plugins(md, &Options::default(), &plugins) @@ -32,7 +32,13 @@ fn to_html_with_options<'a>(env: Env<'a>, md: &str, options: ExOptions) -> NifRe }; match options.features.syntax_highlight_theme { Some(theme) => { - let inkjet_adapter = InkjetAdapter::new(&theme); + let inkjet_adapter = InkjetAdapter::new( + &theme, + options + .features + .syntax_highlight_inline_style + .unwrap_or(true), + ); let mut plugins = ComrakPlugins::default(); plugins.render.codefence_syntax_highlighter = Some(&inkjet_adapter); let unsafe_html = markdown_to_html_with_plugins(md, &comrak_options, &plugins); diff --git a/native/comrak_nif/src/types/options.rs b/native/comrak_nif/src/types/options.rs index 5976d39..b88e3c6 100644 --- a/native/comrak_nif/src/types/options.rs +++ b/native/comrak_nif/src/types/options.rs @@ -66,6 +66,7 @@ pub struct ExRenderOptions { pub struct ExFeaturesOptions { pub sanitize: bool, pub syntax_highlight_theme: Option, + pub syntax_highlight_inline_style: Option, } #[derive(Debug, NifStruct)] diff --git a/test/mdex_test.exs b/test/mdex_test.exs index 1be3e69..abbb044 100644 --- a/test/mdex_test.exs +++ b/test/mdex_test.exs @@ -80,6 +80,21 @@ defmodule MDExTest do """ ) end + + test "without inline styles" do + assert_output( + ~S""" + ```elixir + {:mdex, "~> 0.1"} + ``` + """, + ~S""" +
{:mdex, "~> 0.1"}
+        
+ """, + features: [syntax_highlight_inline_style: false] + ) + end end test "render emoji shortcodes" do