Skip to content

Commit cc460cb

Browse files
committed
Refactor to split lib
1 parent e3d0236 commit cc460cb

31 files changed

+1219
-745
lines changed

index.js

Lines changed: 1 addition & 745 deletions
Large diffs are not rendered by default.

lib/all.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2015 Titus Wormer
4+
* @license MIT
5+
* @module mdast:to-hast:all
6+
* @fileoverview Visit all MDAST nodes.
7+
*/
8+
9+
'use strict';
10+
11+
/* eslint-env commonjs */
12+
13+
/* Expose. */
14+
module.exports = all;
15+
16+
/* Dependencies. */
17+
var trim = require('trim');
18+
var one = require('./one');
19+
20+
/**
21+
* Transform the children of `parent`.
22+
*
23+
* @param {Function} h - Hyperscript DSL.
24+
* @param {Node} parent - Parent to visit.
25+
* @return {Array.<Node>} - HAST nodes.
26+
*/
27+
function all(h, parent) {
28+
var nodes = parent.children || [];
29+
var length = nodes.length;
30+
var values = [];
31+
var index = -1;
32+
var result;
33+
var head;
34+
35+
while (++index < length) {
36+
result = one(h, nodes[index], parent);
37+
38+
if (result) {
39+
if (index && nodes[index - 1].type === 'break') {
40+
if (result.value) {
41+
result.value = trim.left(result.value);
42+
}
43+
44+
head = result.children && result.children[0];
45+
46+
if (head && head.value) {
47+
head.value = trim.left(head.value);
48+
}
49+
}
50+
51+
values = values.concat(result);
52+
}
53+
}
54+
55+
return values;
56+
}

lib/failsafe.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2015 Titus Wormer
4+
* @license MIT
5+
* @module mdast:to-hast:failsafe
6+
* @fileoverview Failsafe to stringify references
7+
* without definitions back to markdown.
8+
*/
9+
10+
'use strict';
11+
12+
/* eslint-env commonjs */
13+
14+
/* Expose. */
15+
module.exports = failsafe;
16+
17+
/* Dependencies. */
18+
var u = require('unist-builder');
19+
var all = require('./all');
20+
21+
/**
22+
* Return the content of a reference without definition
23+
* as markdown.
24+
*
25+
* @param {Function} h - Hyperscript DSL.
26+
* @param {Node} node - Node to compile.
27+
* @param {Node?} [definition] - Definition node.
28+
* @return {Array.<string>?} - Node, list of nodes, or nothing.
29+
*/
30+
function failsafe(h, node, definition) {
31+
var subtype = node.referenceType;
32+
33+
if (subtype !== 'collapsed' && subtype !== 'full' && !definition) {
34+
if (node.type === 'imageReference') {
35+
return u('text', '![' + node.alt + ']');
36+
}
37+
38+
return [u('text', '[')].concat(all(h, node), u('text', ']'));
39+
}
40+
}

lib/footer.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2015 Titus Wormer
4+
* @license MIT
5+
* @module mdast:to-hast:footnotes
6+
* @fileoverview Generate the footnote footer.
7+
*/
8+
9+
'use strict';
10+
11+
/* eslint-env commonjs */
12+
13+
/* Expose. */
14+
module.exports = generateFootnotes;
15+
16+
/* Dependencies. */
17+
var thematicBreak = require('./handlers/thematic-break');
18+
var list = require('./handlers/list');
19+
var wrap = require('./wrap');
20+
21+
/**
22+
* Transform all footnote definitions, if any.
23+
*
24+
* @param {Function} h - Hyperscript DSL.
25+
* @return {Node?} - Compiled footnotes, if any.
26+
*/
27+
function generateFootnotes(h) {
28+
var footnotes = h.footnotes;
29+
var length = footnotes.length;
30+
var index = -1;
31+
var listItems = [];
32+
var def;
33+
34+
if (!length) {
35+
return null;
36+
}
37+
38+
while (++index < length) {
39+
def = footnotes[index];
40+
41+
listItems[index] = {
42+
type: 'listItem',
43+
data: {hProperties: {id: 'fn-' + def.identifier}},
44+
children: def.children.concat({
45+
type: 'link',
46+
url: '#fnref-' + def.identifier,
47+
data: {hProperties: {className: ['footnote-backref']}},
48+
children: [{
49+
type: 'text',
50+
value: '↩'
51+
}]
52+
}),
53+
position: def.position
54+
};
55+
}
56+
57+
return h(null, 'div', {
58+
className: ['footnotes']
59+
}, wrap([
60+
thematicBreak(h),
61+
list(h, {
62+
type: 'list',
63+
ordered: true,
64+
children: listItems
65+
})
66+
], true));
67+
}

lib/handlers/blockquote.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2015 Titus Wormer
4+
* @license MIT
5+
* @module mdast:to-hast:handlers:blockquote
6+
* @fileoverview Handle a `blockquote`.
7+
*/
8+
9+
'use strict';
10+
11+
/* eslint-env commonjs */
12+
13+
/* Expose. */
14+
module.exports = blockquote;
15+
16+
/* Dependencies. */
17+
var wrap = require('../wrap');
18+
var all = require('../all');
19+
20+
/**
21+
* Transform a block quote.
22+
*
23+
* @param {Function} h - Hyperscript DSL.
24+
* @param {Node} node - Node to compile.
25+
* @return {Node} - HAST node.
26+
*/
27+
function blockquote(h, node) {
28+
return h(node, 'blockquote', wrap(all(h, node), true));
29+
}

lib/handlers/break.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2015 Titus Wormer
4+
* @license MIT
5+
* @module mdast:to-hast:handlers:break
6+
* @fileoverview Handle a `break`.
7+
*/
8+
9+
'use strict';
10+
11+
/* eslint-env commonjs */
12+
13+
/* Expose. */
14+
module.exports = hardBreak;
15+
16+
/* Dependencies. */
17+
var u = require('unist-builder');
18+
19+
/**
20+
* Transform an inline break.
21+
*
22+
* @param {Function} h - Hyperscript DSL.
23+
* @param {Node} node - Node to compile.
24+
* @return {Array.<Node>} - HAST nodes.
25+
*/
26+
function hardBreak(h, node) {
27+
return [h(node, 'br'), u('text', '\n')];
28+
}

lib/handlers/code.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2015 Titus Wormer
4+
* @license MIT
5+
* @module mdast:to-hast:handlers:code
6+
* @fileoverview Handle `code`.
7+
*/
8+
9+
'use strict';
10+
11+
/* eslint-env commonjs */
12+
13+
/* Expose. */
14+
module.exports = code;
15+
16+
/* Dependencies. */
17+
var detab = require('detab');
18+
var u = require('unist-builder');
19+
20+
/**
21+
* Transform a code block.
22+
*
23+
* @param {Function} h - Hyperscript DSL.
24+
* @param {Node} node - Node to compile.
25+
* @return {Node} - HAST node.
26+
*/
27+
function code(h, node) {
28+
var value = node.value ? detab(node.value + '\n') : '';
29+
var lang = node.lang && node.lang.match(/^[^ \t]+(?=[ \t]|$)/);
30+
var props = {};
31+
32+
if (lang) {
33+
props.className = ['language-' + lang];
34+
}
35+
36+
return h(node.position, 'pre', [
37+
h(node, 'code', props, [u('text', value)])
38+
]);
39+
}

lib/handlers/delete.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2015 Titus Wormer
4+
* @license MIT
5+
* @module mdast:to-hast:handlers:delete
6+
* @fileoverview Handle `delete`.
7+
*/
8+
9+
'use strict';
10+
11+
/* eslint-env commonjs */
12+
13+
/* Expose. */
14+
module.exports = strikethrough;
15+
16+
/* Dependencies. */
17+
var all = require('../all');
18+
19+
/**
20+
* Transform deletions.
21+
*
22+
* @param {Function} h - Hyperscript DSL.
23+
* @param {Node} node - Node to compile.
24+
* @return {Node} - HAST node.
25+
*/
26+
function strikethrough(h, node) {
27+
return h(node, 'del', all(h, node));
28+
}

lib/handlers/emphasis.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2015 Titus Wormer
4+
* @license MIT
5+
* @module mdast:to-hast:handlers:emphasis
6+
* @fileoverview Handle `emphasis`.
7+
*/
8+
9+
'use strict';
10+
11+
/* eslint-env commonjs */
12+
13+
/* Expose. */
14+
module.exports = emphasis;
15+
16+
/* Dependencies. */
17+
var all = require('../all');
18+
19+
/**
20+
* Transform emphasis
21+
*
22+
* @param {Function} h - Hyperscript DSL.
23+
* @param {Node} node - Node to compile.
24+
* @return {Node} - HAST node.
25+
*/
26+
function emphasis(h, node) {
27+
return h(node, 'em', all(h, node));
28+
}

lib/handlers/footnote-reference.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2015 Titus Wormer
4+
* @license MIT
5+
* @module mdast:to-hast:handlers:footnote-reference
6+
* @fileoverview Handle `footnoteReference`.
7+
*/
8+
9+
'use strict';
10+
11+
/* eslint-env commonjs */
12+
13+
/* Expose. */
14+
module.exports = footnoteReference;
15+
16+
/* Dependencies. */
17+
var u = require('unist-builder');
18+
19+
/**
20+
* Transform a reference to a footnote.
21+
*
22+
* @param {Function} h - Hyperscript DSL.
23+
* @param {Node} node - Node to compile.
24+
* @return {Node} - HAST node.
25+
*/
26+
function footnoteReference(h, node) {
27+
var identifier = node.identifier;
28+
29+
return h(node.position, 'sup', {id: 'fnref-' + identifier}, [
30+
h(node, 'a', {
31+
href: '#fn-' + identifier,
32+
className: ['footnote-ref']
33+
}, [u('text', identifier)])
34+
]);
35+
}

0 commit comments

Comments
 (0)