kramdown-parser-gfm-extractions 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +22 -0
- data/README.md +66 -0
- data/Rakefile +11 -0
- data/kramdown-parser-gfm-extractions.gemspec +21 -0
- data/lib/kramdown-parser-gfm-extractions.rb +1 -0
- data/lib/kramdown-parser-gfm-extractions/version.rb +3 -0
- data/lib/kramdown/parser/gfm_extractions.rb +48 -0
- metadata +81 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: da211446b52c5373ae22fb0ea908fb3250f24352b849e7dac76340d2ec4c49eb
|
4
|
+
data.tar.gz: e34dd49cbdcb77cec604fdcb39b4ab6874cc0b95527322a448135c946689a68a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5d2760aad0d74402513b01cb385336dfa701e99b2d474292bf2f6c98f788b7fbf59815ac040ba96fbac91217d468743fc2d7b2f0cabb1d3bdbee880f22c81f12
|
7
|
+
data.tar.gz: a78ec16946531a2fecc3d53c0f3d9f276732ebde158811c53646a134c051796a0c3bf887a94270d157631fe8bd91abd41224ccb2024f80ebc55d218f900a5351
|
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021-present Jared White and Bridgetown contributors
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# kramdown-parser-gfm-extractions
|
2
|
+
|
3
|
+
A [Kramdown](https://kramdown.gettalong.org) parser extension which provides support for extracting fenced code blocks featuring meta (for example `js script` or `html preview-story`). Useful for supporting the [Markdown JavaScript (mdjs) format](https://rocket.modern-web.dev/docs/markdown-javascript/overview/).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Run this command to add the gem to your project's Gemfile.
|
8
|
+
|
9
|
+
```
|
10
|
+
bundle add kramdown-parser-gfm-extractions
|
11
|
+
```
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
Simply require the gem, pass the relevant input option to Kramdown, and you'll get the appropriate HTML output as well as an array of extractions.
|
16
|
+
|
17
|
+
~~~ruby
|
18
|
+
require "kramdown"
|
19
|
+
require "kramdown-parser-gfm-extractions"
|
20
|
+
|
21
|
+
text = <<~MD
|
22
|
+
Hello **folks**.
|
23
|
+
|
24
|
+
```js script
|
25
|
+
import "Foo" from "./bar.js"
|
26
|
+
```
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
a = 1 + 2
|
30
|
+
```
|
31
|
+
|
32
|
+
```html preview-story
|
33
|
+
<p>I'm a preview!</p>
|
34
|
+
```
|
35
|
+
|
36
|
+
This is _Markdown!_
|
37
|
+
MD
|
38
|
+
|
39
|
+
doc = Kramdown::Document.new(text, {input: :GFMExtractions})
|
40
|
+
html = doc.to_html
|
41
|
+
extractions = doc.root.options[:extractions]
|
42
|
+
~~~
|
43
|
+
|
44
|
+
In this example, the `js script` block and the `html preview-story` block would both be extracted. In the list of extractions available via `doc.root.options[:extractions]`, you'd obtain hashes with the following keys:
|
45
|
+
|
46
|
+
* **lang** - the language code (js, html, etc.)
|
47
|
+
* **meta** - the meta string (script, preview-story, etc.)
|
48
|
+
* **code** - the actual code block verbatim
|
49
|
+
|
50
|
+
By default, the extracted code is still output to the HTML but contained within a custom tag called `kramdown-extraction` containing an inert `template` tag with the rendered output of the syntax processor. If you wish to customize or turn off this behavior, pass these options along to `Kramdown::Document`:
|
51
|
+
|
52
|
+
* **include_extraction_tags** - set to `false` to entirely remove the extraction tags
|
53
|
+
* **include_code_in_extractions** - set to `false` to strip the rendered code templates out of the extraction tags (but still keep the tags themselves)
|
54
|
+
|
55
|
+
## Testing
|
56
|
+
|
57
|
+
* Run `bundle exec rake test` to run the test suite
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
1. Fork it (https://github.com/bridgetownrb/kramdown-parser-gfm-extractions/fork)
|
62
|
+
2. Clone the fork using `git clone` to your local development machine.
|
63
|
+
3. Create your feature branch (`git checkout -b my-new-feature`)
|
64
|
+
4. Commit your changes (`git commit -am 'Add some feature'`)
|
65
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
66
|
+
6. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/kramdown-parser-gfm-extractions/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "kramdown-parser-gfm-extractions"
|
7
|
+
spec.version = KramdownParserGFMExtractions::VERSION
|
8
|
+
spec.author = "Bridgetown Team"
|
9
|
+
spec.email = "maintainers@bridgetownrb.com"
|
10
|
+
spec.summary = "A subclass of Kramdown's GFM parser which extracts fenced code blocks featuring meta (aka `js script`)"
|
11
|
+
spec.homepage = "https://github.com/bridgetownrb/kramdown-parser-gfm-extractions"
|
12
|
+
spec.license = "MIT"
|
13
|
+
|
14
|
+
spec.required_ruby_version = ">= 2.5"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r!^(test|script|spec|features)/!) }
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_dependency("kramdown", "~> 2.0")
|
20
|
+
spec.add_dependency("kramdown-parser-gfm", "~> 1.0")
|
21
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative "kramdown/parser/gfm_extractions"
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require "kramdown/parser/gfm"
|
2
|
+
require "digest/sha2"
|
3
|
+
|
4
|
+
module Kramdown
|
5
|
+
module Parser
|
6
|
+
class GFMExtractions < Kramdown::Parser::GFM
|
7
|
+
FENCED_CODEBLOCK_MATCH = /^[ ]{0,3}(([~`]){3,})\s*?((.+?)(?:\?\S*)?)?\s*?\n(.*?)^[ ]{0,3}\1\2*\s*?\n/m.freeze
|
8
|
+
|
9
|
+
def parse_codeblock_fenced
|
10
|
+
if @src.check(FENCED_CODEBLOCK_MATCH) && @src[3].to_s.strip.include?(" ")
|
11
|
+
@src.pos += @src.matched_size
|
12
|
+
lang = @src[3].to_s.strip
|
13
|
+
lang = lang.split(" ")
|
14
|
+
|
15
|
+
sha = Digest::SHA2.hexdigest(@src[5])
|
16
|
+
@root.options[:extractions] ||= []
|
17
|
+
@root.options[:extractions] << {
|
18
|
+
sha: sha,
|
19
|
+
lang: lang[0],
|
20
|
+
meta: lang[1],
|
21
|
+
code: @src[5]
|
22
|
+
}
|
23
|
+
|
24
|
+
start_line_number = @src.current_line_number
|
25
|
+
|
26
|
+
unless options[:include_extraction_tags] == false
|
27
|
+
el = Element.new(:html_element, "kramdown-extraction", {id: "ex-#{sha}", lang: lang[0], meta: lang[1]}, category: :block, location: start_line_number, content_model: :raw)
|
28
|
+
|
29
|
+
unless options[:include_code_in_extractions] == false
|
30
|
+
code_el = new_block_el(:codeblock, @src[5], nil, location: start_line_number, fenced: true)
|
31
|
+
code_el.options[:lang] = lang[0]
|
32
|
+
code_el.attr['class'] = "language-#{lang[0]}"
|
33
|
+
el.children << Element.new(:html_element, "template", nil, category: :block, location: start_line_number, content_model: :raw).tap do |tmpl|
|
34
|
+
tmpl.children << code_el
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
@tree.children << el
|
39
|
+
end
|
40
|
+
|
41
|
+
true
|
42
|
+
else
|
43
|
+
super
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kramdown-parser-gfm-extractions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bridgetown Team
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-04-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: kramdown
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: kramdown-parser-gfm
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
41
|
+
description:
|
42
|
+
email: maintainers@bridgetownrb.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- ".gitignore"
|
48
|
+
- CHANGELOG.md
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- kramdown-parser-gfm-extractions.gemspec
|
54
|
+
- lib/kramdown-parser-gfm-extractions.rb
|
55
|
+
- lib/kramdown-parser-gfm-extractions/version.rb
|
56
|
+
- lib/kramdown/parser/gfm_extractions.rb
|
57
|
+
homepage: https://github.com/bridgetownrb/kramdown-parser-gfm-extractions
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '2.5'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubygems_version: 3.1.4
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: A subclass of Kramdown's GFM parser which extracts fenced code blocks featuring
|
80
|
+
meta (aka `js script`)
|
81
|
+
test_files: []
|