-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
142 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,33 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
var toString = require('mdast-util-to-string'); | ||
var visit = require('unist-util-visit'); | ||
var slugs = require('github-slugger')(); | ||
var toString = require('mdast-util-to-string') | ||
var visit = require('unist-util-visit') | ||
var slugs = require('github-slugger')() | ||
|
||
module.exports = slug; | ||
module.exports = slug | ||
|
||
function slug() { | ||
return transformer; | ||
return transformer | ||
} | ||
|
||
/* Patch slugs on heading nodes. */ | ||
function transformer(ast) { | ||
slugs.reset(); | ||
slugs.reset() | ||
|
||
visit(ast, 'heading', visitor); | ||
visit(ast, 'heading', visitor) | ||
|
||
function visitor(node) { | ||
var id = slugs.slug(toString(node)); | ||
var id = slugs.slug(toString(node)) | ||
|
||
if (!node.data) { | ||
node.data = {}; | ||
node.data = {} | ||
} | ||
|
||
if (!node.data.hProperties) { | ||
node.data.hProperties = {}; | ||
node.data.hProperties = {} | ||
} | ||
|
||
node.data.id = id; | ||
node.data.hProperties.id = id; | ||
node.data.id = id | ||
node.data.hProperties.id = id | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,143 +1,144 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
var test = require('tape'); | ||
var remark = require('remark'); | ||
var u = require('unist-builder'); | ||
var removePosition = require('unist-util-remove-position'); | ||
var slug = require('.'); | ||
var test = require('tape') | ||
var remark = require('remark') | ||
var u = require('unist-builder') | ||
var removePosition = require('unist-util-remove-position') | ||
var slug = require('.') | ||
|
||
function process(doc, options) { | ||
var processor = remark().use(slug, options); | ||
var tree = processor.runSync(processor.parse(doc)); | ||
var processor = remark().use(slug, options) | ||
|
||
removePosition(tree, true); | ||
|
||
return tree; | ||
return removePosition(processor.runSync(processor.parse(doc)), true) | ||
} | ||
|
||
test('remark-slug', function (t) { | ||
var processor = remark().use(slug); | ||
var ast; | ||
test('remark-slug', function(t) { | ||
var processor = remark().use(slug) | ||
var ast | ||
|
||
ast = process([ | ||
'# Normal', | ||
'', | ||
'## Table of Contents', | ||
'', | ||
'# Baz', | ||
'' | ||
].join('\n')); | ||
ast = process( | ||
['# Normal', '', '## Table of Contents', '', '# Baz', ''].join('\n') | ||
) | ||
|
||
t.deepEqual( | ||
[ast.children[0].data.id, ast.children[1].data.id, ast.children[2].data.id], | ||
['normal', 'table-of-contents', 'baz'], | ||
'should patch `id`s' | ||
); | ||
) | ||
|
||
t.deepEqual( | ||
[ast.children[0].data.hProperties.id, ast.children[1].data.hProperties.id, ast.children[2].data.hProperties.id], | ||
[ | ||
ast.children[0].data.hProperties.id, | ||
ast.children[1].data.hProperties.id, | ||
ast.children[2].data.hProperties.id | ||
], | ||
['normal', 'table-of-contents', 'baz'], | ||
'should patch `hProperties.id`s' | ||
); | ||
) | ||
|
||
ast = processor.parse('# Normal', {position: false}); | ||
ast = processor.parse('# Normal', {position: false}) | ||
|
||
ast.children[0].data = {foo: 'bar'}; | ||
ast.children[0].data = {foo: 'bar'} | ||
|
||
processor.run(ast); | ||
processor.run(ast) | ||
|
||
t.equal( | ||
ast.children[0].data.foo, | ||
'bar', | ||
'should not overwrite `data` on headings' | ||
); | ||
) | ||
|
||
ast = processor.parse('# Normal', {position: false}); | ||
ast = processor.parse('# Normal', {position: false}) | ||
|
||
ast.children[0].data = { | ||
hProperties: {className: 'bar'} | ||
}; | ||
} | ||
|
||
processor.run(ast); | ||
processor.run(ast) | ||
|
||
t.equal( | ||
ast.children[0].data.hProperties.className, | ||
'bar', | ||
'should not overwrite `data.hProperties` on headings' | ||
); | ||
|
||
t.deepEqual(process([ | ||
'## I ♥ unicode', | ||
'', | ||
'## Dash-dash', | ||
'', | ||
'## en–dash', | ||
'', | ||
'## em–dash', | ||
'', | ||
'## 😄 unicode emoji', | ||
'', | ||
'## 😄-😄 unicode emoji', | ||
'', | ||
'## 😄_😄 unicode emoji', | ||
'', | ||
'##', | ||
'', | ||
'## ', | ||
'', | ||
'## Initial spaces', | ||
'', | ||
'## Final spaces ', | ||
'', | ||
'## Duplicate', | ||
'', | ||
'## Duplicate', | ||
'', | ||
'## :ok: No underscore', | ||
'', | ||
'## :ok_hand: Single', | ||
'', | ||
'## :ok_hand::hatched_chick: Two in a row with no spaces', | ||
'', | ||
'## :ok_hand: :hatched_chick: Two in a row', | ||
'' | ||
].join('\n')), | ||
u('root', [ | ||
heading('I ♥ unicode', 'i--unicode'), | ||
heading('Dash-dash', 'dash-dash'), | ||
heading('en–dash', 'endash'), | ||
heading('em–dash', 'emdash'), | ||
heading('😄 unicode emoji', '-unicode-emoji'), | ||
heading('😄-😄 unicode emoji', '--unicode-emoji'), | ||
heading('😄_😄 unicode emoji', '_-unicode-emoji'), | ||
heading(null, ''), | ||
heading(null, '-1'), | ||
heading('Initial spaces', 'initial-spaces'), | ||
heading('Final spaces', 'final-spaces'), | ||
heading('Duplicate', 'duplicate'), | ||
heading('Duplicate', 'duplicate-1'), | ||
heading(':ok: No underscore', 'ok-no-underscore'), | ||
heading(':ok_hand: Single', 'ok_hand-single'), | ||
heading( | ||
':ok_hand::hatched_chick: Two ' + | ||
'in a row with no spaces', | ||
'ok_handhatched_chick-two-in-a-row-' + | ||
'with-no-spaces' | ||
), | ||
heading( | ||
':ok_hand: :hatched_chick: Two in a row', | ||
'ok_hand-hatched_chick-two-in-a-row' | ||
) | ||
]), | ||
'should create GitHub slugs' | ||
); | ||
) | ||
|
||
t.end(); | ||
}); | ||
t.deepEqual( | ||
process( | ||
[ | ||
'## I ♥ unicode', | ||
'', | ||
'## Dash-dash', | ||
'', | ||
'## en–dash', | ||
'', | ||
'## em–dash', | ||
'', | ||
'## 😄 unicode emoji', | ||
'', | ||
'## 😄-😄 unicode emoji', | ||
'', | ||
'## 😄_😄 unicode emoji', | ||
'', | ||
'##', | ||
'', | ||
'## ', | ||
'', | ||
'## Initial spaces', | ||
'', | ||
'## Final spaces ', | ||
'', | ||
'## Duplicate', | ||
'', | ||
'## Duplicate', | ||
'', | ||
'## :ok: No underscore', | ||
'', | ||
'## :ok_hand: Single', | ||
'', | ||
'## :ok_hand::hatched_chick: Two in a row with no spaces', | ||
'', | ||
'## :ok_hand: :hatched_chick: Two in a row', | ||
'' | ||
].join('\n') | ||
), | ||
u('root', [ | ||
heading('I ♥ unicode', 'i--unicode'), | ||
heading('Dash-dash', 'dash-dash'), | ||
heading('en–dash', 'endash'), | ||
heading('em–dash', 'emdash'), | ||
heading('😄 unicode emoji', '-unicode-emoji'), | ||
heading('😄-😄 unicode emoji', '--unicode-emoji'), | ||
heading('😄_😄 unicode emoji', '_-unicode-emoji'), | ||
heading(null, ''), | ||
heading(null, '-1'), | ||
heading('Initial spaces', 'initial-spaces'), | ||
heading('Final spaces', 'final-spaces'), | ||
heading('Duplicate', 'duplicate'), | ||
heading('Duplicate', 'duplicate-1'), | ||
heading(':ok: No underscore', 'ok-no-underscore'), | ||
heading(':ok_hand: Single', 'ok_hand-single'), | ||
heading( | ||
':ok_hand::hatched_chick: Two in a row with no spaces', | ||
'ok_handhatched_chick-two-in-a-row-with-no-spaces' | ||
), | ||
heading( | ||
':ok_hand: :hatched_chick: Two in a row', | ||
'ok_hand-hatched_chick-two-in-a-row' | ||
) | ||
]), | ||
'should create GitHub slugs' | ||
) | ||
|
||
t.end() | ||
}) | ||
|
||
function heading(label, id) { | ||
return u('heading', { | ||
depth: 2, | ||
data: {id: id, hProperties: {id: id}} | ||
}, label ? [u('text', label)] : []); | ||
return u( | ||
'heading', | ||
{ | ||
depth: 2, | ||
data: {id: id, hProperties: {id: id}} | ||
}, | ||
label ? [u('text', label)] : [] | ||
) | ||
} |