qiita-markdown 0.35.0 → 0.39.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: 23da6f6f9b37dafcbe18164c13d4531998cfca14e0891801b4e8a1fdab6d625c
4
- data.tar.gz: 620e9a413d9d0649511a883d40c4a398260f739a2c01774645d1e717421d54f5
3
+ metadata.gz: fdacf171e87076c382a0a48c3470a46e7334965d3818a7b4041c8bd6d2a9af6c
4
+ data.tar.gz: e97066c81cca4c192b97de5956e6e7615a3946bcf5e3790d04d00908829a16f7
5
5
  SHA512:
6
- metadata.gz: f2cfa06b888dd9e08a22a88b822a7a280afec1289c1f9f3833c64692e37edc3b637c26739ab1f607c2e4cab1da1559510bc7a63836e462dc3c4dcc81f4da8343
7
- data.tar.gz: 125ff10aa432f848e8c790f497f2467f1f825431b1cdeed1a085f14da230fdc5800c752a64b86f90d5e4acf36fc4f4c8b9e2efd30f808cee6ac713b32ffc0b01
6
+ metadata.gz: 10b64682c5df3911eb1674315695b9798ac34f6877f553c477a4dfe67a43bcd2a731f4bbc6f24ef52b3930aeee527bd23469c8d691c0f3ab3fcb40e294c552ea
7
+ data.tar.gz: 1eca7b100285a88879a580ced3f868bc620b06c1181850e6082f3ac53ef7f7263db8da514b33c315ee402511d7c83ad1a7b20cd0ed4406efe1d08f23e9e5f645
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  ## Unreleased
2
2
 
3
+ ## 0.39.0
4
+ - Fix an error when custom block type is empty
5
+
6
+ ## 0.38.0
7
+ - Change default syntax highlighter from pygments to rouge
8
+
9
+ ## 0.37.0
10
+ - Change keyword of notation
11
+
12
+ ## 0.36.0
13
+ - Support message notation
14
+
3
15
  ## 0.35.0
4
16
 
5
17
  - Allow Relative URL in iframe src attributes
@@ -0,0 +1,65 @@
1
+ module Qiita
2
+ module Markdown
3
+ module Filters
4
+ class CustomBlock < HTML::Pipeline::Filter
5
+ ALLOWED_TYPES = %w[note].freeze
6
+
7
+ def call
8
+ doc.search('div[data-type="customblock"]').each do |div|
9
+ metadata = Metadata.new(div["data-metadata"])
10
+ next unless ALLOWED_TYPES.include?(metadata.type)
11
+ klass = Object.const_get("#{self.class}::#{metadata.type.capitalize}")
12
+ klass.new(div, metadata.subtype).convert
13
+ end
14
+ doc
15
+ end
16
+
17
+ class Metadata
18
+ attr_reader :type, :subtype
19
+
20
+ # @param text [String, nil]
21
+ # @note Attribute `type` will be nil if `text` is nil
22
+ # @note Attribute `subtype` will be nil if `text` does not include white space.
23
+ def initialize(text)
24
+ # Discared after the second word.
25
+ @type, @subtype = text && text.split(" ")
26
+ end
27
+ end
28
+
29
+ class Note
30
+ attr_reader :node, :type
31
+
32
+ ALLOWED_TYPES = %w[info warn alert].freeze
33
+ DEFAULT_TYPE = "info".freeze
34
+
35
+ # @param node [Nokogiri::XML::Node]
36
+ # @param type [String, nil]
37
+ def initialize(node, type)
38
+ @node = node
39
+ @type = ALLOWED_TYPES.include?(type) ? type : DEFAULT_TYPE
40
+ end
41
+
42
+ def convert
43
+ node.inner_html = message
44
+ node["class"] = "note #{type}"
45
+ node.children.first.add_previous_sibling(icon) if icon
46
+ end
47
+
48
+ private
49
+
50
+ def message
51
+ "<p>#{node.text}</p>"
52
+ end
53
+
54
+ def icon
55
+ {
56
+ info: %(<span class="fa fa-fw fa-check-circle"></span>),
57
+ warn: %(<span class="fa fa-fw fa-exclamation-circle"></span>),
58
+ alert: %(<span class="fa fa-fw fa-times-circle"></span>),
59
+ }[type.to_sym]
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -20,6 +20,7 @@ module Qiita
20
20
  Qiita::Markdown::Greenmat::HTMLRenderer.new(hard_wrap: true, with_toc_data: true),
21
21
  autolink: true,
22
22
  fenced_code_blocks: true,
23
+ fenced_custom_blocks: true,
23
24
  footnotes: options[:footnotes],
24
25
  no_intra_emphasis: true,
25
26
  no_mention_emphasis: true,
@@ -4,6 +4,7 @@ module Qiita
4
4
  class SyntaxHighlight < HTML::Pipeline::Filter
5
5
  DEFAULT_LANGUAGE = "text"
6
6
  DEFAULT_TIMEOUT = Float::INFINITY
7
+ DEFAULT_OPTION = "html_legacy"
7
8
 
8
9
  def call
9
10
  elapsed = 0
@@ -79,11 +80,11 @@ module Qiita
79
80
  end
80
81
 
81
82
  def highlight(language)
82
- Pygments.highlight(code, lexer: language, options: pygments_options)
83
+ Rouge.highlight(code, language, DEFAULT_OPTION)
83
84
  end
84
85
 
85
86
  def highlighted_node
86
- if specific_language && Pygments::Lexer.find(specific_language)
87
+ if specific_language && Rouge::Lexer.find(specific_language)
87
88
  begin
88
89
  highlight(specific_language).presence or raise
89
90
  rescue
@@ -102,14 +103,6 @@ module Qiita
102
103
  Nokogiri::HTML.fragment(%Q[<div class="code-frame" data-lang="#{language}"></div>])
103
104
  end
104
105
 
105
- def pygments_options
106
- @pygments_options ||= begin
107
- options = { encoding: "utf-8", stripnl: false }
108
- options[:startinline] = true if has_inline_php?
109
- options
110
- end
111
- end
112
-
113
106
  def specific_language
114
107
  @specific_language || @node["lang"]
115
108
  end
@@ -13,7 +13,7 @@ module Qiita
13
13
  "a" => %w[class href rel title],
14
14
  "blockquote" => %w[cite] + Embed::Tweet::ATTRIBUTES,
15
15
  "code" => %w[data-metadata],
16
- "div" => %w[class],
16
+ "div" => %w[class data-type data-metadata],
17
17
  "font" => %w[color],
18
18
  "h1" => %w[id],
19
19
  "h2" => %w[id],
@@ -14,6 +14,7 @@ module Qiita
14
14
  Filters::ImageLink,
15
15
  Filters::Footnote,
16
16
  Filters::CodeBlock,
17
+ Filters::CustomBlock,
17
18
  Filters::Checkbox,
18
19
  Filters::Toc,
19
20
  Filters::Emoji,
@@ -1,5 +1,5 @@
1
1
  module Qiita
2
2
  module Markdown
3
- VERSION = "0.35.0"
3
+ VERSION = "0.39.0"
4
4
  end
5
5
  end
@@ -4,7 +4,7 @@ require "html/pipeline"
4
4
  require "linguist"
5
5
  require "mem"
6
6
  require "nokogiri"
7
- require "pygments"
7
+ require "rouge"
8
8
  require "sanitize"
9
9
 
10
10
  require "qiita/markdown/embed/code_pen"
@@ -20,6 +20,7 @@ require "qiita/markdown/transformers/filter_iframe"
20
20
  require "qiita/markdown/transformers/strip_invalid_node"
21
21
  require "qiita/markdown/filters/checkbox"
22
22
  require "qiita/markdown/filters/code_block"
23
+ require "qiita/markdown/filters/custom_block"
23
24
  require "qiita/markdown/filters/emoji"
24
25
  require "qiita/markdown/filters/external_link"
25
26
  require "qiita/markdown/filters/final_sanitizer"
@@ -22,8 +22,8 @@ Gem::Specification.new do |spec|
22
22
  spec.add_dependency "github-linguist", "~> 4.0"
23
23
  spec.add_dependency "html-pipeline", "~> 2.0"
24
24
  spec.add_dependency "mem"
25
- spec.add_dependency "pygments.rb", "~> 1.0"
26
- spec.add_dependency "greenmat", "3.5.1.1"
25
+ spec.add_dependency "rouge", "3.26.0"
26
+ spec.add_dependency "greenmat", "3.5.1.2"
27
27
  spec.add_dependency "sanitize"
28
28
  spec.add_dependency "addressable"
29
29
  spec.add_development_dependency "activesupport", "4.2.6"
@@ -149,8 +149,8 @@ describe Qiita::Markdown::Processor do
149
149
  should eq <<-HTML.strip_heredoc
150
150
  <div class="code-frame" data-lang="ruby">
151
151
  <div class="code-lang"><span class="bold">example.rb</span></div>
152
- <div class="highlight"><pre><span></span><span class="mi">1</span>
153
- </pre></div>
152
+ <div class="highlight"><pre class="codehilite"><code><span class="mi">1</span>
153
+ </code></pre></div>
154
154
  </div>
155
155
  HTML
156
156
  end
@@ -169,8 +169,8 @@ describe Qiita::Markdown::Processor do
169
169
  should eq <<-HTML.strip_heredoc
170
170
  <div class="code-frame" data-lang="php">
171
171
  <div class="code-lang"><span class="bold">example.php</span></div>
172
- <div class="highlight"><pre><span></span><span class="mi">1</span>
173
- </pre></div>
172
+ <div class="highlight"><pre class="codehilite"><code><span class="mi">1</span>
173
+ </code></pre></div>
174
174
  </div>
175
175
  HTML
176
176
  end
@@ -187,8 +187,8 @@ describe Qiita::Markdown::Processor do
187
187
 
188
188
  it "returns code-frame and highlighted pre element" do
189
189
  should eq <<-HTML.strip_heredoc
190
- <div class="code-frame" data-lang="ruby"><div class="highlight"><pre><span></span><span class="mi">1</span>
191
- </pre></div></div>
190
+ <div class="code-frame" data-lang="ruby"><div class="highlight"><pre class="codehilite"><code><span class="mi">1</span>
191
+ </code></pre></div></div>
192
192
  HTML
193
193
  end
194
194
  end
@@ -226,10 +226,10 @@ describe Qiita::Markdown::Processor do
226
226
 
227
227
  it "does not strip the newlines" do
228
228
  should eq <<-HTML.strip_heredoc
229
- <div class="code-frame" data-lang="text"><div class="highlight"><pre><span></span>
229
+ <div class="code-frame" data-lang="text"><div class="highlight"><pre class="codehilite"><code>
230
230
  foo
231
231
 
232
- </pre></div></div>
232
+ </code></pre></div></div>
233
233
  HTML
234
234
  end
235
235
  end
@@ -311,8 +311,8 @@ describe Qiita::Markdown::Processor do
311
311
  should include(<<-HTML.strip_heredoc.rstrip)
312
312
  <div class="code-frame" data-lang="ruby">
313
313
  <div class="code-lang"><span class="bold">@alice</span></div>
314
- <div class="highlight"><pre><span></span><span class="mi">1</span>
315
- </pre></div>
314
+ <div class="highlight"><pre class="codehilite"><code><span class="mi">1</span>
315
+ </code></pre></div>
316
316
  </div>
317
317
  HTML
318
318
  end
@@ -659,9 +659,9 @@ describe Qiita::Markdown::Processor do
659
659
 
660
660
  it "does not replace checkbox" do
661
661
  should eq <<-HTML.strip_heredoc
662
- <div class="code-frame" data-lang="text"><div class="highlight"><pre><span></span>- [ ] a
662
+ <div class="code-frame" data-lang="text"><div class="highlight"><pre class="codehilite"><code>- [ ] a
663
663
  - [x] b
664
- </pre></div></div>
664
+ </code></pre></div></div>
665
665
  HTML
666
666
  end
667
667
  end
@@ -768,9 +768,9 @@ describe Qiita::Markdown::Processor do
768
768
 
769
769
  it "generates only code blocks without footnotes" do
770
770
  should eq <<-HTML.strip_heredoc
771
- <div class="code-frame" data-lang="text"><div class="highlight"><pre><span></span>[^1]
771
+ <div class="code-frame" data-lang="text"><div class="highlight"><pre class="codehilite"><code>[^1]
772
772
  [^1]: test
773
- </pre></div></div>
773
+ </code></pre></div></div>
774
774
  HTML
775
775
  end
776
776
  end
@@ -1067,8 +1067,8 @@ describe Qiita::Markdown::Processor do
1067
1067
  expect(subject).to eq <<-HTML.strip_heredoc
1068
1068
  <p><details><summary>Folding sample</summary><div>
1069
1069
 
1070
- <div class="code-frame" data-lang="rb"><div class="highlight"><pre><span></span><span class="nb">puts</span> <span class="s2">"Hello, World"</span>
1071
- </pre></div></div>
1070
+ <div class="code-frame" data-lang="rb"><div class="highlight"><pre class="codehilite"><code><span class="nb">puts</span> <span class="s2">"Hello, World"</span>
1071
+ </code></pre></div></div>
1072
1072
 
1073
1073
  <p></p>
1074
1074
  </div></details></p>
@@ -1124,8 +1124,8 @@ describe Qiita::Markdown::Processor do
1124
1124
  should eq <<-HTML.strip_heredoc
1125
1125
  <div class="code-frame" data-lang="js">
1126
1126
  <div class="code-lang"><span class="bold">test<script>alert(1)</script></span></div>
1127
- <div class="highlight"><pre><span></span><span class="mi">1</span>
1128
- </pre></div>
1127
+ <div class="highlight"><pre class="codehilite"><code><span class="mi">1</span>
1128
+ </code></pre></div>
1129
1129
  </div>
1130
1130
  HTML
1131
1131
  end
@@ -1134,8 +1134,8 @@ describe Qiita::Markdown::Processor do
1134
1134
  should eq <<-HTML.strip_heredoc
1135
1135
  <div class="code-frame" data-lang="js">
1136
1136
  <div class="code-lang"><span class="bold">test</span></div>
1137
- <div class="highlight"><pre><span></span><span class="mi">1</span>
1138
- </pre></div>
1137
+ <div class="highlight"><pre class="codehilite"><code><span class="mi">1</span>
1138
+ </code></pre></div>
1139
1139
  </div>
1140
1140
  HTML
1141
1141
  end
@@ -1677,6 +1677,133 @@ describe Qiita::Markdown::Processor do
1677
1677
  end
1678
1678
  end
1679
1679
 
1680
+ shared_examples_for "custom block" do |allowed:|
1681
+ context "with custom block" do
1682
+ let(:type) { "" }
1683
+ let(:subtype) { "" }
1684
+
1685
+ let(:markdown) do
1686
+ <<-MARKDOWN.strip_heredoc
1687
+ :::#{[type, subtype].join(' ').rstrip}
1688
+ Some kind of text is here.
1689
+ :::
1690
+ MARKDOWN
1691
+ end
1692
+
1693
+ context "when type is empty" do
1694
+ if allowed
1695
+ it "returns simple div element" do
1696
+ should eq <<-HTML.strip_heredoc
1697
+ <div data-type="customblock">Some kind of text is here.
1698
+ </div>
1699
+ HTML
1700
+ end
1701
+ else
1702
+ it "returns simple div element" do
1703
+ should eq <<-HTML.strip_heredoc
1704
+ <div>Some kind of text is here.
1705
+ </div>
1706
+ HTML
1707
+ end
1708
+ end
1709
+ end
1710
+
1711
+ context "when type is not allowed" do
1712
+ let(:type) { "anytype" }
1713
+
1714
+ if allowed
1715
+ it "returns simple div element" do
1716
+ should eq <<-HTML.strip_heredoc
1717
+ <div data-type="customblock" data-metadata="anytype">Some kind of text is here.
1718
+ </div>
1719
+ HTML
1720
+ end
1721
+ else
1722
+ it "returns simple div element" do
1723
+ should eq <<-HTML.strip_heredoc
1724
+ <div>Some kind of text is here.
1725
+ </div>
1726
+ HTML
1727
+ end
1728
+ end
1729
+ end
1730
+
1731
+ context "when type is note" do
1732
+ let(:type) { "note" }
1733
+
1734
+ context "when subtype is empty" do
1735
+ if allowed
1736
+ it "returns info note block with class including icon as default type" do
1737
+ should eq <<-HTML.strip_heredoc
1738
+ <div data-type="customblock" data-metadata="note" class="note info">
1739
+ <span class="fa fa-fw fa-check-circle"></span><p>Some kind of text is here.
1740
+ </p>
1741
+ </div>
1742
+ HTML
1743
+ end
1744
+ else
1745
+ it "returns note block with class including icon" do
1746
+ should eq <<-HTML.strip_heredoc
1747
+ <div class="note info">
1748
+ <span class="fa fa-fw fa-check-circle"></span><p>Some kind of text is here.
1749
+ </p>
1750
+ </div>
1751
+ HTML
1752
+ end
1753
+ end
1754
+ end
1755
+
1756
+ context "when subtype is warn" do
1757
+ let(:subtype) { "warn" }
1758
+
1759
+ if allowed
1760
+ it "returns warning note block with class including icon" do
1761
+ should eq <<-HTML.strip_heredoc
1762
+ <div data-type="customblock" data-metadata="note warn" class="note warn">
1763
+ <span class="fa fa-fw fa-exclamation-circle"></span><p>Some kind of text is here.
1764
+ </p>
1765
+ </div>
1766
+ HTML
1767
+ end
1768
+ else
1769
+ it "returns note block with class including icon" do
1770
+ should eq <<-HTML.strip_heredoc
1771
+ <div class="note warn">
1772
+ <span class="fa fa-fw fa-exclamation-circle"></span><p>Some kind of text is here.
1773
+ </p>
1774
+ </div>
1775
+ HTML
1776
+ end
1777
+ end
1778
+ end
1779
+
1780
+ context "when subtype is alert" do
1781
+ let(:subtype) { "alert" }
1782
+
1783
+ if allowed
1784
+ it "returns alerting note block with class including icon" do
1785
+ should eq <<-HTML.strip_heredoc
1786
+ <div data-type="customblock" data-metadata="note alert" class="note alert">
1787
+ <span class="fa fa-fw fa-times-circle"></span><p>Some kind of text is here.
1788
+ </p>
1789
+ </div>
1790
+ HTML
1791
+ end
1792
+ else
1793
+ it "returns note block with class including icon" do
1794
+ should eq <<-HTML.strip_heredoc
1795
+ <div class="note alert">
1796
+ <span class="fa fa-fw fa-times-circle"></span><p>Some kind of text is here.
1797
+ </p>
1798
+ </div>
1799
+ HTML
1800
+ end
1801
+ end
1802
+ end
1803
+ end
1804
+ end
1805
+ end
1806
+
1680
1807
  context "without script and strict context" do
1681
1808
  let(:context) do
1682
1809
  super().merge(script: false, strict: false)
@@ -1691,6 +1818,7 @@ describe Qiita::Markdown::Processor do
1691
1818
  include_examples "class attribute", allowed: true
1692
1819
  include_examples "background-color", allowed: true
1693
1820
  include_examples "override embed code attributes", allowed: false
1821
+ include_examples "custom block", allowed: false
1694
1822
  end
1695
1823
 
1696
1824
  context "with script context" do
@@ -1707,6 +1835,7 @@ describe Qiita::Markdown::Processor do
1707
1835
  include_examples "class attribute", allowed: true
1708
1836
  include_examples "background-color", allowed: true
1709
1837
  include_examples "override embed code attributes", allowed: true
1838
+ include_examples "custom block", allowed: true
1710
1839
  end
1711
1840
 
1712
1841
  context "with strict context" do
@@ -1723,6 +1852,7 @@ describe Qiita::Markdown::Processor do
1723
1852
  include_examples "class attribute", allowed: false
1724
1853
  include_examples "background-color", allowed: false
1725
1854
  include_examples "override embed code attributes", allowed: false
1855
+ include_examples "custom block", allowed: false
1726
1856
  end
1727
1857
 
1728
1858
  context "with script and strict context" do
@@ -1739,6 +1869,7 @@ describe Qiita::Markdown::Processor do
1739
1869
  include_examples "class attribute", allowed: false
1740
1870
  include_examples "background-color", allowed: false
1741
1871
  include_examples "override embed code attributes", allowed: false
1872
+ include_examples "custom block", allowed: true
1742
1873
  end
1743
1874
  end
1744
1875
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qiita-markdown
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.35.0
4
+ version: 0.39.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-05 00:00:00.000000000 Z
11
+ date: 2021-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gemoji
@@ -67,33 +67,33 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: pygments.rb
70
+ name: rouge
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - '='
74
74
  - !ruby/object:Gem::Version
75
- version: '1.0'
75
+ version: 3.26.0
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - '='
81
81
  - !ruby/object:Gem::Version
82
- version: '1.0'
82
+ version: 3.26.0
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: greenmat
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - '='
88
88
  - !ruby/object:Gem::Version
89
- version: 3.5.1.1
89
+ version: 3.5.1.2
90
90
  type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - '='
95
95
  - !ruby/object:Gem::Version
96
- version: 3.5.1.1
96
+ version: 3.5.1.2
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: sanitize
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -265,6 +265,7 @@ files:
265
265
  - lib/qiita/markdown/embed/youtube.rb
266
266
  - lib/qiita/markdown/filters/checkbox.rb
267
267
  - lib/qiita/markdown/filters/code_block.rb
268
+ - lib/qiita/markdown/filters/custom_block.rb
268
269
  - lib/qiita/markdown/filters/emoji.rb
269
270
  - lib/qiita/markdown/filters/external_link.rb
270
271
  - lib/qiita/markdown/filters/final_sanitizer.rb
@@ -315,7 +316,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
315
316
  - !ruby/object:Gem::Version
316
317
  version: '0'
317
318
  requirements: []
318
- rubygems_version: 3.0.3
319
+ rubygems_version: 3.1.4
319
320
  signing_key:
320
321
  specification_version: 4
321
322
  summary: Qiita-specified markdown processor.