Skip to content

Commit 9ec60b3

Browse files
committedNov 27, 2017
init
1 parent dfefd9a commit 9ec60b3

File tree

7 files changed

+5565
-0
lines changed

7 files changed

+5565
-0
lines changed
 

‎.travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- '4.0'
4+
- '9.0'
5+
after_script: bash <(curl -s https://codecov.io/bash)

‎license.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Paul Zimmer
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎package-lock.json

+5,360
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"name": "remark-code-blocks",
3+
"version": "1.0.0",
4+
"description": "Extract code blocks from an MDAST tree",
5+
"main": "index.js",
6+
"scripts": {
7+
"clean": "rimraf ./*.js .nyc_output coverage",
8+
"pretest": "npm run babel",
9+
"tap": "tap -Rspec test.js",
10+
"babel": "babel src -d .",
11+
"test": "nyc npm run tap"
12+
},
13+
"babel": {
14+
"presets": [
15+
[
16+
"env",
17+
{
18+
"targets": {
19+
"node": "current"
20+
}
21+
}
22+
]
23+
]
24+
},
25+
"repository": {
26+
"type": "git",
27+
"url": "git+https://github.com/mrzmmr/remark-code-blocks.git"
28+
},
29+
"keywords": [
30+
"remark",
31+
"util",
32+
"utility",
33+
"code",
34+
"blocks",
35+
"extract"
36+
],
37+
"author": "mrzmmr",
38+
"license": "MIT",
39+
"bugs": {
40+
"url": "https://github.com/mrzmmr/remark-code-blocks/issues"
41+
},
42+
"homepage": "https://github.com/mrzmmr/remark-code-blocks#readme",
43+
"devDependencies": {
44+
"babel-cli": "^6.26.0",
45+
"babel-preset-env": "^1.6.1",
46+
"nyc": "^11.3.0",
47+
"remark": "^8.0.0",
48+
"rimraf": "^2.6.2",
49+
"tap": "^10.7.3",
50+
"unist-util-is": "^2.1.1"
51+
},
52+
"dependencies": {
53+
"unist-util-find-all-after": "^1.0.1"
54+
}
55+
}

‎readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# remark-code-blocks

‎src/index.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict'
2+
3+
/**
4+
* Remark code blocks
5+
*
6+
* Extracts code blocks from an mdast tree and stores them in vfiles data object.
7+
*/
8+
9+
const findAllAfter = require('unist-util-find-all-after')
10+
11+
module.exports = (options = {}) => {
12+
return (tree, file) => {
13+
const { values, lang } = options
14+
let codeblocks
15+
let nodes = []
16+
17+
nodes = findAllAfter(tree, 0, 'code')
18+
19+
if (tree.children[0].type === 'code') {
20+
nodes.unshift(tree.children[0])
21+
}
22+
23+
codeblocks = nodes
24+
25+
if (lang) {
26+
nodes = codeblocks
27+
codeblocks = nodes.filter(node => node.lang === lang)
28+
}
29+
30+
if (values) {
31+
nodes = codeblocks
32+
codeblocks = nodes.map(node => node.value)
33+
}
34+
35+
file.data.codeblocks = codeblocks
36+
}
37+
}

‎src/test.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use strict'
2+
3+
const is = require('unist-util-is')
4+
const remark = require('remark')
5+
const test = require('tap').test
6+
const codeblocks = require('./')
7+
8+
const markdown = `
9+
# Foo
10+
11+
\`\`\`js
12+
const inc = n => n++;
13+
\`\`\`
14+
15+
\`\`\`
16+
inc(42);
17+
\`\`\`
18+
`
19+
20+
const code = markdown.split('\n').filter((n, i) => i === 4 || i === 8)
21+
22+
test('remark-code-blocks', t => {
23+
let result
24+
let file
25+
26+
t.test('should find all `code` nodes including parent.children[0]', it => {
27+
it.doesNotThrow(() => {
28+
file = remark()
29+
.use(codeblocks)
30+
.processSync(
31+
markdown
32+
.split('\n')
33+
.splice(2)
34+
.join('\n')
35+
)
36+
result = file.data.codeblocks
37+
38+
it.ok(is(`code`, result[0]), '`code` as first child is included')
39+
it.ok(result[0].value === code[0], '`code` as first child is included')
40+
})
41+
it.end()
42+
})
43+
44+
t.test('with no options passed', it => {
45+
it.doesNotThrow(() => {
46+
file = remark()
47+
.use(codeblocks)
48+
.processSync(markdown)
49+
result = file.data.codeblocks
50+
51+
it.ok(file, 'should process')
52+
it.ok(Array.isArray(result), 'result should be an array')
53+
it.ok(is('code', result[0]), 'result[i] should be type `code`')
54+
it.ok(is('code', result[1]), 'results[i] should be type `code`')
55+
}, 'should not throw')
56+
it.end()
57+
})
58+
59+
t.test('with lang option', it => {
60+
it.doesNotThrow(() => {
61+
file = remark()
62+
.use(codeblocks, { lang: 'js' })
63+
.processSync(markdown)
64+
result = file.data.codeblocks
65+
66+
it.ok(result.length === 1, 'result should have length of 1')
67+
it.ok(result[0].value === code[0], 'result should be first value code[0]')
68+
}, 'should not throw')
69+
it.end()
70+
})
71+
72+
t.test('with values option', it => {
73+
it.doesNotThrow(() => {
74+
file = remark()
75+
.use(codeblocks, { values: 1 })
76+
.processSync(markdown)
77+
result = file.data.codeblocks
78+
79+
it.ok(result[0] === code[0], 'result should be string and match code[i]')
80+
it.ok(result[1] === code[1], 'result should be string and match code[i]')
81+
}, 'should not throw')
82+
it.end()
83+
})
84+
85+
t.end()
86+
})

0 commit comments

Comments
 (0)
Please sign in to comment.