middleman-commonmarker 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2843575c04f2b516147d5aaaa62789482ead5e3e65950ac268b824759883c1bf
4
- data.tar.gz: 9e90c64fba5895575d957c0d9ad174254f7242d06e648405f7a84b01e9e7703c
3
+ metadata.gz: 7938f5a8afb2d9e01bb037c4b62fefcc9f5b84a92a4886b33134a95055fbc32e
4
+ data.tar.gz: 0f0fd975884df03a1a25cde357c61bd87230430b6ac683ecea52c8b8c1dee876
5
5
  SHA512:
6
- metadata.gz: 18e343279365e8a11a2ace792f8441cf5f4f8753c40d4a8772389117937346428d8461474c77888cca55dc117e95277f62e64d54dc0bca77aaa735bf999cf49f
7
- data.tar.gz: 60024dfa0cc18d349f5b93f8d2a1ddaa2b60e6cba42fa83c53b710196d6268db4629ef24360d71d2ff511f876804c1e80f12b4066a026e90bd1ba0f9e4600672
6
+ metadata.gz: 35421d08c87e2ef6de283ac036951ebca03136bbb4a0f08610ccf5f4252618563287d89d70936545447857ab6630387a375dc035bf25f9659bca8fd760e9da09
7
+ data.tar.gz: decc3cedac0273958f0dbe2854a2f6a9df04349253dd80c7290aff344c4767a0d57fe13acaa1fb62519f6d9d2cb97f17bf0205decb27e42081a7380bbf48c9c5
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.0] - 2026-12-05
4
+
5
+ - Support commonmarker 3.0 ([#32](https://github.com/unasuke/middleman-commonmarker/pull/32)). This follows commonmarker 3.0's new defaults, which changes the rendered HTML:
6
+ - Fenced code blocks are no longer syntax highlighted unless `plugins` is set. Set `plugins: {syntax_highlighter: {}}` to get the previous `base16-ocean.dark` highlighting back.
7
+ - Backslash-escaped characters are no longer wrapped in `<span data-escaped-char>`. Set `escaped_char_spans: true` (newly accepted in `set :markdown` and `activate :commonmarker, options:`) to get the previous output back.
8
+ - Invalid `plugins` values raise `TypeError` (instead of `NoMethodError` for a non-Hash), and `plugins: {syntax_highlighter: {}}` now enables the default theme instead of raising.
9
+
10
+ ## [0.2.0] - 2026-10-25
11
+
12
+ - Add `plugins` option to pass commonmarker plugins (e.g. syntax highlighter theme) ([#18](https://github.com/unasuke/middleman-commonmarker/pull/18))
13
+
3
14
  ## [0.1.0] - 2026-05-27
4
15
 
5
16
  - Initial release
data/README.md CHANGED
@@ -46,6 +46,32 @@ Note that flat keys on `activate` (e.g. `activate :commonmarker, table: true`) a
46
46
 
47
47
  These flat options are mapped onto commonmarker's `parse` / `render` / `extension` option groups (and `smartypants` maps to `parse.smart`).
48
48
 
49
+ ### Syntax highlighting
50
+
51
+ Fenced code blocks are not syntax highlighted by default (commonmarker 3.0 and later turn the highlighter off unless you ask for it).
52
+
53
+ Use the `plugins` option to enable it. Its value is passed as-is to commonmarker's `plugins:` argument; see [commonmarker's README](https://github.com/gjtorikian/commonmarker#plugins) for the available values. The highlighter writes the colors as inline `style` attributes.
54
+
55
+ ```ruby
56
+ # Use the default theme (base16-ocean.dark), as commonmarker 2.x did without any option
57
+ set :markdown, plugins: {syntax_highlighter: {}}
58
+
59
+ # Use another built-in theme
60
+ set :markdown, plugins: {syntax_highlighter: {theme: "InspiredGitHub"}}
61
+
62
+ # Load syntax_themes/my-theme.tmTheme
63
+ set :markdown, plugins: {
64
+ syntax_highlighter: {theme: "my-theme", path: File.expand_path("syntax_themes", __dir__)}
65
+ }
66
+
67
+ # Disable syntax highlighting (the default)
68
+ set :markdown, plugins: {syntax_highlighter: nil}
69
+ ```
70
+
71
+ Give `path` as an absolute path; a relative path is resolved against the current working directory.
72
+
73
+ `plugins` can also be given through `activate :commonmarker, options: { plugins: ... }`. When both are set, the `activate` value replaces the `set :markdown` value entirely (no deep merge).
74
+
49
75
  ### Raw HTML is enabled during rendering
50
76
 
51
77
  To emit the `<img>` HTML produced by `image_tag`, the renderer forces commonmarker's `unsafe: true` during transformation.
@@ -53,7 +79,7 @@ As a result, raw HTML in your Markdown is passed through (consistent with Redcar
53
79
 
54
80
  ## Scope
55
81
 
56
- This extension covers helper integration (`image_tag` / `url_for`), `automatic_image_sizes`, and commonmarker's native parse/render/extension options (`table`, `strikethrough`, `autolink`, `smartypants`, `tasklist`, …).
82
+ This extension covers helper integration (`image_tag` / `url_for`), `automatic_image_sizes`, commonmarker's native parse/render/extension options (`table`, `strikethrough`, `autolink`, `smartypants`, `tasklist`, …), and commonmarker plugins such as the syntax highlighter (`plugins`).
57
83
  Redcarpet-specific options such as `no_images`, `no_links`, `link_attributes`, and `filter_html` are out of scope.
58
84
 
59
85
  ## Development
@@ -75,6 +75,27 @@ Feature: Markdown (Commonmarker) support
75
75
  When I go to "/tables.html"
76
76
  Then I should see "<table>"
77
77
 
78
+ Scenario: Commonmarker passes plugins through set :markdown
79
+ Given a fixture app "markdown-app"
80
+ And a file named "config.rb" with:
81
+ """
82
+ activate :commonmarker
83
+ set :markdown, plugins: {syntax_highlighter: {theme: "InspiredGitHub"}}
84
+ """
85
+ Given the Server is running at "markdown-app"
86
+ When I go to "/fenced_code_blocks.html"
87
+ Then I should see "background-color:#ffffff"
88
+
89
+ Scenario: Commonmarker passes plugins through activate
90
+ Given a fixture app "markdown-app"
91
+ And a file named "config.rb" with:
92
+ """
93
+ activate :commonmarker, options: {plugins: {syntax_highlighter: {theme: "InspiredGitHub"}}}
94
+ """
95
+ Given the Server is running at "markdown-app"
96
+ When I go to "/fenced_code_blocks.html"
97
+ Then I should see "background-color:#ffffff"
98
+
78
99
  Scenario: Commonmarker uses our link_to and image_tag helpers
79
100
  Given a fixture app "markdown-app"
80
101
  And a file named "config.rb" with:
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "commonmarker"
4
4
  require "active_support/core_ext/module/attribute_accessors"
5
+ require "active_support/core_ext/hash/keys"
5
6
 
6
7
  module Middleman
7
8
  module Renderers
@@ -13,7 +14,8 @@ module Middleman
13
14
 
14
15
  DEFAULT_PARSE_OPTIONS = {smart: false, default_info_string: nil}.freeze
15
16
  DEFAULT_RENDER_OPTIONS = {hardbreaks: false, github_pre_lang: false, width: 80,
16
- unsafe: false, escape: false, sourcepos: false}.freeze
17
+ unsafe: false, escape: false, sourcepos: false,
18
+ escaped_char_spans: false}.freeze
17
19
  DEFAULT_EXTENSION_OPTIONS = {strikethrough: false, tagfilter: false, table: false,
18
20
  autolink: false, tasklist: false, superscript: false,
19
21
  header_ids: nil, footnotes: false, description_lists: false,
@@ -21,6 +23,7 @@ module Middleman
21
23
 
22
24
  def prepare
23
25
  @context = options.delete(:context)
26
+ @commonmarker_plugins = build_commonmarker_plugins
24
27
  @commonmarker_options = build_commonmarker_options
25
28
  end
26
29
 
@@ -36,10 +39,10 @@ module Middleman
36
39
  end
37
40
  end
38
41
  # Force unsafe so the injected raw <img> HTML is emitted.
39
- return doc.to_html(options: render_options_with_unsafe)
42
+ return render_html(doc, render_options_with_unsafe)
40
43
  end
41
44
 
42
- doc.to_html(options: @commonmarker_options)
45
+ render_html(doc, @commonmarker_options)
43
46
  end
44
47
 
45
48
  private
@@ -77,12 +80,32 @@ module Middleman
77
80
  nil
78
81
  end
79
82
 
83
+ # Render with the user's `plugins` when given; otherwise let commonmarker
84
+ # apply its default plugins.
85
+ def render_html(doc, commonmarker_options)
86
+ doc.to_html(options: commonmarker_options, **@commonmarker_plugins)
87
+ end
88
+
80
89
  def render_options_with_unsafe
81
90
  @commonmarker_options.merge(
82
91
  render: @commonmarker_options[:render].merge(unsafe: true)
83
92
  )
84
93
  end
85
94
 
95
+ # Take `plugins` out of the options as keyword arguments for to_html: {}
96
+ # when unset, {plugins: value} when set (a nil value disables the syntax
97
+ # highlighter). When both :plugins and "plugins" are present, the later
98
+ # one wins, as with the flat options, so `activate` overrides
99
+ # `set :markdown`.
100
+ def build_commonmarker_plugins
101
+ keys = options.keys.select { |key| key.to_s == "plugins" }
102
+ return {} if keys.empty?
103
+
104
+ value = keys.map { |key| options.delete(key) }.last
105
+ value = value.deep_symbolize_keys if value.is_a?(Hash)
106
+ {plugins: value}
107
+ end
108
+
86
109
  # Distribute the flat `set :markdown, ...` options into the
87
110
  # parse/render/extension nested structure, mapping `smartypants` to
88
111
  # parse.smart.
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Middleman
4
4
  module Commonmarker
5
- VERSION = "0.1.0"
5
+ VERSION = "0.3.0"
6
6
  end
7
7
  end
@@ -16,7 +16,9 @@ module Middleman
16
16
  def transform_link_node: (untyped node) -> void
17
17
  def extract_text: (untyped node) -> String
18
18
  def html_node_from: (String html) -> untyped
19
+ def render_html: (untyped doc, Hash[Symbol, untyped] commonmarker_options) -> String
19
20
  def render_options_with_unsafe: () -> Hash[Symbol, untyped]
21
+ def build_commonmarker_plugins: () -> Hash[Symbol, untyped]
20
22
  def build_commonmarker_options: () -> Hash[Symbol, untyped]
21
23
  end
22
24
  end
@@ -1,18 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "test_helper"
4
+ require "pathname"
4
5
 
5
6
  class CommonmarkerTemplateTest < Minitest::Test
6
- # Build a template instance; prepare runs on init and computes
7
- # @commonmarker_options from the given options.
8
- def build(opts = {})
9
- Middleman::Renderers::CommonmarkerTemplate.new(nil, 1, opts) { "" }
7
+ CODE_BLOCK = "```ruby\nputs 1\n```\n"
8
+ THEMES_DIR = File.expand_path("fixtures", __dir__)
9
+
10
+ # Build a template instance whose source is `body`; prepare runs on init
11
+ # and computes @commonmarker_plugins and @commonmarker_options from the
12
+ # given options.
13
+ def build(opts = {}, body = "")
14
+ Middleman::Renderers::CommonmarkerTemplate.new(nil, 1, opts) { body }
10
15
  end
11
16
 
12
17
  def options_for(opts)
13
18
  build(opts).instance_variable_get(:@commonmarker_options)
14
19
  end
15
20
 
21
+ # Render CODE_BLOCK with the given options. Tilt passes Object.new as the
22
+ # scope, so this goes through the scope branch of evaluate.
23
+ def html_for(opts)
24
+ build(opts, CODE_BLOCK).render
25
+ end
26
+
16
27
  def image_node(markdown)
17
28
  node = Commonmarker.parse(markdown)
18
29
  found = nil
@@ -26,6 +37,16 @@ class CommonmarkerTemplateTest < Minitest::Test
26
37
  assert_equal true, opts[:render][:hardbreaks]
27
38
  end
28
39
 
40
+ def test_escaped_char_spans_is_off_by_default
41
+ assert_equal "<p>a * b</p>\n", build({}, "a \\* b\n").render
42
+ end
43
+
44
+ def test_escaped_char_spans_maps_to_render
45
+ assert_equal true, options_for(escaped_char_spans: true)[:render][:escaped_char_spans]
46
+ assert_equal "<p>a <span data-escaped-char>*</span> b</p>\n",
47
+ build({escaped_char_spans: true}, "a \\* b\n").render
48
+ end
49
+
29
50
  def test_smartypants_maps_to_parse_smart
30
51
  assert_equal true, options_for(smartypants: true)[:parse][:smart]
31
52
  end
@@ -53,4 +74,90 @@ class CommonmarkerTemplateTest < Minitest::Test
53
74
  def test_html_node_from_returns_nil_when_no_html_present
54
75
  assert_nil build.send(:html_node_from, "just plain text")
55
76
  end
77
+
78
+ def test_plugins_unset_matches_commonmarker_default
79
+ template = build({}, CODE_BLOCK)
80
+ opts = template.send(:render_options_with_unsafe)
81
+ expected = Commonmarker.parse(CODE_BLOCK, options: opts).to_html(options: opts)
82
+ assert_equal expected, template.render
83
+ refute_includes template.render, "style="
84
+ end
85
+
86
+ def test_plugins_with_builtin_theme
87
+ html = html_for(plugins: {syntax_highlighter: {theme: "InspiredGitHub"}})
88
+ assert_includes html, "background-color:#ffffff"
89
+ end
90
+
91
+ def test_plugins_with_theme_from_path
92
+ html = html_for(plugins: {syntax_highlighter: {theme: "test-theme", path: THEMES_DIR}})
93
+ assert_includes html, "background-color:#123456"
94
+ end
95
+
96
+ def test_plugins_with_path_only_uses_default_theme
97
+ html = html_for(plugins: {syntax_highlighter: {path: THEMES_DIR}})
98
+ assert_includes html, "background-color:#2b303b"
99
+ end
100
+
101
+ def test_plugins_with_empty_theme_outputs_classes
102
+ html = html_for(plugins: {syntax_highlighter: {theme: ""}})
103
+ assert_includes html, %(<pre class="syntax-highlighting">)
104
+ refute_includes html, "style="
105
+ end
106
+
107
+ def test_plugins_disabling_highlighter
108
+ [{syntax_highlighter: nil}, {}, nil].each do |plugins|
109
+ refute_includes html_for(plugins:), "style=", "plugins: #{plugins.inspect}"
110
+ end
111
+ end
112
+
113
+ def test_plugins_with_misspelled_key_disables_highlighter
114
+ refute_includes html_for(plugins: {syntax_highlight: {theme: "InspiredGitHub"}}), "style="
115
+ end
116
+
117
+ def test_plugins_with_string_keys
118
+ assert_equal html_for(plugins: {syntax_highlighter: {theme: "InspiredGitHub"}}),
119
+ html_for(plugins: {"syntax_highlighter" => {"theme" => "InspiredGitHub"}})
120
+ end
121
+
122
+ def test_later_plugins_key_takes_precedence
123
+ inspired = {syntax_highlighter: {theme: "InspiredGitHub"}}
124
+ assert_equal html_for(plugins: inspired), html_for({:plugins => nil, "plugins" => inspired})
125
+ assert_equal html_for(plugins: nil), html_for({"plugins" => inspired, :plugins => nil})
126
+ end
127
+
128
+ def test_plugins_with_unknown_theme_raises
129
+ assert_raises(ArgumentError) { html_for(plugins: {syntax_highlighter: {theme: "no-such-theme"}}) }
130
+ end
131
+
132
+ def test_plugins_with_empty_highlighter_uses_default_theme
133
+ assert_includes html_for(plugins: {syntax_highlighter: {}}), "background-color:#2b303b"
134
+ end
135
+
136
+ def test_plugins_with_nil_theme_raises
137
+ assert_raises(TypeError) { html_for(plugins: {syntax_highlighter: {theme: nil}}) }
138
+ end
139
+
140
+ def test_plugins_with_non_hash_value_raises
141
+ ["x", [1]].each do |plugins|
142
+ assert_raises(TypeError, "plugins: #{plugins.inspect}") { html_for(plugins:) }
143
+ end
144
+ end
145
+
146
+ def test_plugins_with_pathname_path_raises
147
+ assert_raises(TypeError) do
148
+ html_for(plugins: {syntax_highlighter: {theme: "test-theme", path: Pathname(THEMES_DIR)}})
149
+ end
150
+ end
151
+
152
+ def test_plugins_are_not_mixed_into_commonmarker_options
153
+ opts = options_for(plugins: {syntax_highlighter: {theme: "InspiredGitHub"}})
154
+ refute opts[:parse].key?(:plugins)
155
+ refute opts[:render].key?(:plugins)
156
+ refute opts[:extension].key?(:plugins)
157
+ end
158
+
159
+ def test_plugins_apply_without_scope
160
+ template = build({plugins: {syntax_highlighter: {theme: "InspiredGitHub"}}}, CODE_BLOCK)
161
+ assert_includes template.send(:evaluate, nil, {}), "background-color:#ffffff"
162
+ end
56
163
  end
@@ -0,0 +1,20 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
+ <plist version="1.0">
4
+ <dict>
5
+ <key>name</key>
6
+ <string>test-theme</string>
7
+ <key>settings</key>
8
+ <array>
9
+ <dict>
10
+ <key>settings</key>
11
+ <dict>
12
+ <key>background</key>
13
+ <string>#123456</string>
14
+ <key>foreground</key>
15
+ <string>#abcdef</string>
16
+ </dict>
17
+ </dict>
18
+ </array>
19
+ </dict>
20
+ </plist>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: middleman-commonmarker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yusuke Nakamura
@@ -85,6 +85,7 @@ files:
85
85
  - sig/middleman-commonmarker/extension.rbs
86
86
  - sig/middleman-commonmarker/version.rbs
87
87
  - test/commonmarker_template_test.rb
88
+ - test/fixtures/test-theme.tmTheme
88
89
  - test/test_helper.rb
89
90
  homepage: https://github.com/unasuke/middleman-commonmarker
90
91
  licenses:
@@ -109,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
110
  - !ruby/object:Gem::Version
110
111
  version: '0'
111
112
  requirements: []
112
- rubygems_version: 4.0.10
113
+ rubygems_version: 4.0.20
113
114
  specification_version: 4
114
115
  summary: Commonmarker (CommonMark) markdown engine extension for Middleman
115
116
  test_files: []