kramdown-components 0.0.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +130 -5
- data/kramdown-components.gemspec +6 -6
- data/lib/kramdown/custom_document.rb +10 -38
- data/lib/kramdown/custom_element.rb +3 -4
- metadata +11 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 88fc82100145b4d17743313e5975016be37fbee2887a415ac784d5443b42ef43
|
4
|
+
data.tar.gz: 423666e61c2b28eb8ed764a688cfb3c0ec4e260ca7932810f32dcf075ef28217
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 83c176d9d2e3affcf7f33a128550fb9a8af84b2193c2a39cca6d704d790a8ddba3be314e82b84e96c6b995def86fe438ab7911a4707c4c71c8a7351c9af48e17
|
7
|
+
data.tar.gz: ef7a2f3d053ab444fb87044f0ffe26a3ca8f929b03f23b753a199f99a333def509aa9e6ff568ef317be04183d8633e76712aedd3cfbb47084c0f632da2f49b20
|
data/.gitignore
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
kramdown-components-*.gem
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Kramdown Components
|
2
2
|
|
3
|
-
Rewrite sections of Markdown documents using HTML Custom Elements.
|
3
|
+
Rewrite and capture sections of Markdown documents using HTML Custom Elements.
|
4
4
|
|
5
|
-
Useful for enhancing code blocks and formatted markup with more sophisticated controls while maintaining a clean separation of presentation and content.
|
5
|
+
Useful for enhancing code blocks and formatted markup with more sophisticated controls while maintaining a clean separation of presentation and content. Extract content from custom elements.
|
6
6
|
|
7
7
|
## How it Works
|
8
8
|
|
@@ -22,10 +22,135 @@ doc = Kramdown::CustomDocument("# Title")
|
|
22
22
|
puts doc.to_html
|
23
23
|
```
|
24
24
|
|
25
|
-
###
|
25
|
+
### Registering Components
|
26
26
|
|
27
|
-
|
27
|
+
To define a custom element that can wrap child elements with the Markdown document, use the `define_element` class method to set the tag name.
|
28
28
|
|
29
|
+
```ruby
|
30
|
+
Kramdown::CustomDocument.define_element("wrapped-block")
|
31
|
+
```
|
32
|
+
|
33
|
+
You can customise the behaviour of components by extending `CustomElement` and binding the class to the tag name.
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
class ImageGallery < Kramdown::CustomElement; end
|
37
|
+
|
38
|
+
Kramdown::CustomDocument.define_element("image-gallery", ImageGallery)
|
39
|
+
```
|
40
|
+
|
41
|
+
### Rewriting the DOM
|
42
|
+
|
43
|
+
To rewrite DOM subtrees wrapped by the component, implement the `parse_dom` method of `CustomElement` and manipulate the internal `root` and `children` elements, with instances of `Kramdown::Element` to represent each node.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
class ButtonList < Kramdown::CustomElement
|
47
|
+
def parse_dom(root)
|
48
|
+
uls = root.children.filter { |child_el| child_el.type == :ul }
|
49
|
+
uls.each do |ul|
|
50
|
+
ul.children.each do |li|
|
51
|
+
button = Kramdown::Element.new(:html_element, "button")
|
52
|
+
button.children = li.children
|
53
|
+
li.children = [button]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
Kramdown::CustomDocument.define_element("button-list", ButtonList)
|
60
|
+
```
|
61
|
+
|
62
|
+
When this `#parse_dom` hook is applied, all child lists nested within the `<button-list>` component get rewritten to have the content of each list item wrapped in a `<button>`.
|
63
|
+
|
64
|
+
**Source:**
|
65
|
+
|
66
|
+
```md
|
67
|
+
<button-list>
|
68
|
+
|
69
|
+
- One
|
70
|
+
- Two
|
71
|
+
- Three
|
72
|
+
|
73
|
+
</button-list>
|
74
|
+
```
|
75
|
+
|
76
|
+
**Result:**
|
77
|
+
|
78
|
+
```html
|
79
|
+
<button-list id="bl-uftcpsbnzytd">
|
80
|
+
<ul>
|
81
|
+
<li><button>One</button></li>
|
82
|
+
<li><button>Two</button></li>
|
83
|
+
<li><button>Three</button></li>
|
84
|
+
</ul>
|
85
|
+
</button-list>
|
86
|
+
```
|
87
|
+
|
88
|
+
## Extracting Content from the DOM
|
89
|
+
|
90
|
+
It’s also possible to use `#parse_dom` to extract content from the document, with or without rewriting.
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
class Color
|
94
|
+
def self.from_hex(value)
|
95
|
+
r, g, b = value.match(/^#(..)(..)(..)$/).captures.map(&:hex)
|
96
|
+
new(r, g, b)
|
97
|
+
end
|
98
|
+
|
99
|
+
attr_reader :r, :g, :b
|
100
|
+
|
101
|
+
def initialize(r, g, b)
|
102
|
+
@r = r
|
103
|
+
@g = g
|
104
|
+
@b = b
|
105
|
+
end
|
106
|
+
|
107
|
+
def to_css
|
108
|
+
"rgb(#{r}, #{g}, #{b})"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
class ColorSwatch < Kramdown::CustomElement
|
113
|
+
def parse_dom(root)
|
114
|
+
ul = root.children.find { |child_el| child_el.type == :ul }
|
115
|
+
@colors = ul.children.map do |li|
|
116
|
+
li.children.first.children.first.value
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def to_a
|
121
|
+
@colors.map { |hex| Color.from_hex(hex) }
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
Kramdown::CustomDocument.define_element("color-swatch", ColorSwatch)
|
126
|
+
```
|
127
|
+
|
128
|
+
**Source:**
|
129
|
+
|
130
|
+
```md
|
131
|
+
Using the [HoneyGB palette](https://lospec.com/palette-list/honeygb):
|
132
|
+
|
133
|
+
<color-swatch>
|
134
|
+
|
135
|
+
- #3e3a42
|
136
|
+
- #877286
|
137
|
+
- #f0b695
|
138
|
+
- #e9f5da
|
139
|
+
|
140
|
+
</color-swatch>
|
29
141
|
```
|
30
142
|
|
31
|
-
|
143
|
+
**Result:**
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
document = Kramdown::CustomDocument(source)
|
147
|
+
|
148
|
+
# Get a reference to the parsed `ColorSwatch` instance
|
149
|
+
color_swatch = document.custom_elements.first
|
150
|
+
|
151
|
+
# Use the custom `#to_a` method to get the first extracted color
|
152
|
+
color = color_swatch.to_a.first
|
153
|
+
|
154
|
+
# Render the color value as an RGB string
|
155
|
+
color.to_css
|
156
|
+
```
|
data/kramdown-components.gemspec
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = "kramdown-components"
|
3
|
-
spec.version = "0.0
|
3
|
+
spec.version = "0.2.0"
|
4
4
|
spec.platform = Gem::Platform::RUBY
|
5
|
-
spec.summary = "Rewrite sections of Markdown documents using HTML Custom Elements."
|
6
|
-
spec.description = "Mix and match HTML and Markdown syntax. Generate nested DOM trees from a single top level element."
|
5
|
+
spec.summary = "Rewrite and capture sections of Markdown documents using HTML Custom Elements."
|
6
|
+
spec.description = "Mix and match HTML and Markdown syntax. Generate nested DOM trees from a single top level element. Extract content from custom elements."
|
7
7
|
spec.authors = ["Mark Rickerby"]
|
8
8
|
spec.email = "me@maetl.net"
|
9
9
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
10
|
-
spec.add_runtime_dependency "kramdown",
|
11
|
-
spec.add_development_dependency "bundler"
|
12
|
-
spec.add_development_dependency "rspec"
|
10
|
+
spec.add_runtime_dependency "kramdown", "~> 2.4.0"
|
11
|
+
spec.add_development_dependency "bundler", "~> 2.3.22"
|
12
|
+
spec.add_development_dependency "rspec", "~> 3.11.0"
|
13
13
|
#spec.add_development_dependency "rspec-html"
|
14
14
|
spec.homepage = "https://github.com/maetl/kramdown-components"
|
15
15
|
spec.license = "MIT"
|
@@ -13,15 +13,15 @@ module Kramdown
|
|
13
13
|
@@custom_elements[tag_name] = element
|
14
14
|
end
|
15
15
|
|
16
|
+
DEFAULTS = {
|
17
|
+
parse_block_html: true
|
18
|
+
}
|
19
|
+
|
16
20
|
attr_reader :custom_elements
|
17
21
|
|
18
|
-
def initialize(source)
|
22
|
+
def initialize(source, options={})
|
19
23
|
@source = source
|
20
|
-
@parsed_dom = Kramdown::Document.new(@source,
|
21
|
-
input: "GFM",
|
22
|
-
parse_block_html: true,
|
23
|
-
syntax_highlighter: "rouge"
|
24
|
-
})
|
24
|
+
@parsed_dom = Kramdown::Document.new(@source, DEFAULTS.merge(options))
|
25
25
|
@custom_elements = extract_custom_elements
|
26
26
|
end
|
27
27
|
|
@@ -37,25 +37,12 @@ module Kramdown
|
|
37
37
|
@parsed_dom.to_html
|
38
38
|
end
|
39
39
|
|
40
|
-
def has_js?
|
41
|
-
!@custom_elements.empty?
|
42
|
-
end
|
43
|
-
|
44
|
-
def to_js
|
45
|
-
bundle = ["const EXAMPLE_HANDLERS = {}"]
|
46
|
-
|
47
|
-
@custom_elements.each do |element|
|
48
|
-
bundle << element.to_js
|
49
|
-
bundle << "EXAMPLE_HANDLERS[\"#{element.id}\"] = #{element.name}"
|
50
|
-
end
|
51
|
-
|
52
|
-
bundle.join("\n\n")
|
53
|
-
end
|
54
|
-
|
55
40
|
def method_missing(id, *attr, &block)
|
56
41
|
@parsed_dom.send(id, attr, &block)
|
57
42
|
end
|
58
43
|
|
44
|
+
private
|
45
|
+
|
59
46
|
def generate_el_id(tagname)
|
60
47
|
alphabet = [('a'..'z')].map(&:to_a).flatten
|
61
48
|
tag_id = (0...12).map { alphabet[rand(alphabet.length)] }.join
|
@@ -74,23 +61,8 @@ module Kramdown
|
|
74
61
|
end
|
75
62
|
|
76
63
|
custom_element_cls = @@custom_elements[outer_el.value]
|
77
|
-
element = custom_element_cls.new(outer_el.attr["id"]
|
78
|
-
element.parse_dom
|
79
|
-
|
80
|
-
# codeblocks = outer_el.children.filter { |child_el| child_el.type == :codeblock }
|
81
|
-
#
|
82
|
-
# outer_el.children = codeblocks.map do |codeblock|
|
83
|
-
# wrapper = Kramdown::Element.new(
|
84
|
-
# :html_element,
|
85
|
-
# "example-script",
|
86
|
-
# { label: LANG_LABELS[codeblock.options[:lang]] },
|
87
|
-
# { content_model: :block }
|
88
|
-
# )
|
89
|
-
# wrapper.children << codeblock
|
90
|
-
# wrapper
|
91
|
-
# end
|
92
|
-
#
|
93
|
-
# outer_el.children.first.attr[:selected] = true
|
64
|
+
element = custom_element_cls.new(outer_el.attr["id"])
|
65
|
+
element.parse_dom(outer_el)
|
94
66
|
|
95
67
|
elements << element
|
96
68
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kramdown-components
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Rickerby
|
@@ -28,32 +28,32 @@ dependencies:
|
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 2.3.22
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 2.3.22
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 3.11.0
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 3.11.0
|
55
55
|
description: Mix and match HTML and Markdown syntax. Generate nested DOM trees from
|
56
|
-
a single top level element.
|
56
|
+
a single top level element. Extract content from custom elements.
|
57
57
|
email: me@maetl.net
|
58
58
|
executables: []
|
59
59
|
extensions: []
|
@@ -90,5 +90,5 @@ requirements: []
|
|
90
90
|
rubygems_version: 3.1.2
|
91
91
|
signing_key:
|
92
92
|
specification_version: 4
|
93
|
-
summary: Rewrite sections of Markdown documents using HTML Custom Elements.
|
93
|
+
summary: Rewrite and capture sections of Markdown documents using HTML Custom Elements.
|
94
94
|
test_files: []
|