Skip to content

Commit 61b471b

Browse files
authored
Merge pull request #1388 from ojab/add_commonmarker_provider
Add CommonMarker markdown provider
2 parents cfa62ae + 7b7a7d3 commit 61b471b

File tree

5 files changed

+107
-71
lines changed

5 files changed

+107
-71
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ end
1818

1919
group :markdown do
2020
gem 'redcarpet'
21+
gem 'commonmarker'
2122
end
2223

2324
group :textile do

lib/yard/templates/helpers/html_helper.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,19 @@ def htmlify(text, markup = options.markup)
7878
def html_markup_markdown(text)
7979
# TODO: other libraries might be more complex
8080
provider = markup_class(:markdown)
81-
if provider.to_s == 'RDiscount'
81+
case provider.to_s
82+
when 'RDiscount'
8283
provider.new(text, :autolink).to_html
83-
elsif provider.to_s == 'RedcarpetCompat'
84+
when 'RedcarpetCompat'
8485
provider.new(text, :autolink,
8586
:fenced_code,
8687
:gh_blockcode,
8788
:lax_spacing,
8889
:tables,
8990
:with_toc_data,
9091
:no_intraemphasis).to_html
92+
when 'CommonMarker'
93+
CommonMarker.render_html(text, %i[DEFAULT GITHUB_PRE_LANG], %i[autolink])
9194
else
9295
provider.new(text).to_html
9396
end

lib/yard/templates/helpers/markup_helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def clear_markup_cache
2929
{:lib => :bluecloth, :const => 'BlueCloth'},
3030
{:lib => :maruku, :const => 'Maruku'},
3131
{:lib => :'rpeg-markdown', :const => 'PEGMarkdown'},
32-
{:lib => :rdoc, :const => 'YARD::Templates::Helpers::Markup::RDocMarkdown'}
32+
{:lib => :rdoc, :const => 'YARD::Templates::Helpers::Markup::RDocMarkdown'},
33+
{:lib => :commonmarker, :const => 'CommonMarker'}
3334
],
3435
:textile => [
3536
{:lib => :redcloth, :const => 'RedCloth'}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# frozen_string_literal: true
2+
3+
require File.dirname(__FILE__) + '/integration_spec_helper'
4+
5+
RSpec.describe 'Markdown processrors integration' do
6+
include_context 'shared helpers for markup processor integration specs'
7+
8+
shared_examples 'shared examples for markdown processors' do
9+
let(:document) do
10+
<<-MARKDOWN
11+
## Example code listings
12+
13+
Indented block of Ruby code:
14+
15+
x = 1
16+
17+
Fenced block of Ruby code:
18+
19+
```
20+
x = 2
21+
```
22+
23+
Fenced and annotated block of Ruby code:
24+
25+
```ruby
26+
x = 3
27+
```
28+
29+
Fenced and annotated block of non-Ruby code:
30+
31+
```plain
32+
x = 4
33+
```
34+
35+
### Example line break
36+
37+
commonmark line break with\\
38+
a backslash
39+
MARKDOWN
40+
end
41+
42+
it 'renders level 2 header' do
43+
expect(rendered_document).to match(header_regexp(2, 'Example code listings'))
44+
end
45+
46+
it 'renders indented block of code, and applies Ruby syntax highlight' do
47+
expect(rendered_document).to match(highlighted_ruby_regexp('x', '=', '1'))
48+
end
49+
50+
it 'renders fenced block of code, and applies Ruby syntax highlight' do
51+
expect(rendered_document).to match(highlighted_ruby_regexp('x', '=', '2'))
52+
end
53+
54+
it 'renders fenced and annotated block of Ruby code, and applies syntax highlight' do
55+
expect(rendered_document).to match(highlighted_ruby_regexp('x', '=', '3'))
56+
end
57+
58+
it 'renders fenced and annotated block of non-Ruby code, and does not apply syntax highlight' do
59+
expect(rendered_document).to match('x = 4')
60+
end
61+
62+
it "autolinks URLs" do
63+
expect(html_renderer.htmlify('http://example.com', :markdown).chomp.gsub('&#47;', '/')).to eq(
64+
'<p><a href="http://example.com">http://example.com</a></p>'
65+
)
66+
end
67+
end
68+
69+
describe 'Redcarpet' do
70+
let(:markup) { :markdown }
71+
let(:markup_provider) { :redcarpet }
72+
73+
include_examples 'shared examples for markdown processors'
74+
75+
76+
it 'generates anchor tags for level 2 header' do
77+
expect(rendered_document).to include('<h2 id="example-code-listings">Example code listings</h2>')
78+
end
79+
80+
it 'does not create line break via backslash' do
81+
expect(rendered_document).to include("commonmark line break with\\\na backslash")
82+
end
83+
end
84+
85+
describe 'CommonMarker', if: RUBY_VERSION >= '2.3' do
86+
let(:markup) { :markdown }
87+
let(:markup_provider) { :commonmarker }
88+
89+
include_examples 'shared examples for markdown processors'
90+
91+
it 'generates level 2 header without id' do
92+
expect(rendered_document).to include('<h2>Example code listings</h2>')
93+
end
94+
95+
it 'creates line break via backslash' do
96+
expect(rendered_document).to include("commonmark line break with<br />\na backslash")
97+
end
98+
end
99+
end

spec/templates/markup_processor_integrations/redcarpet_spec.rb

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)