-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
203 additions
and
155 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
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,94 +1,102 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
var Index = require('unist-util-index'); | ||
|
||
var url = require('url'), | ||
path = require('path'); | ||
var url = require('url') | ||
var path = require('path') | ||
var Index = require('unist-util-index') | ||
|
||
var concat = [].concat | ||
var push = [].push | ||
|
||
module.exports = defsplit | ||
|
||
function defsplit(opts) { | ||
var id = (opts || {}).id || [] | ||
var ids = (typeof id === 'object' && 'length' in id ? id : [id]).map(String) | ||
|
||
return transform | ||
|
||
function transform(ast) { | ||
var definitionsById = new Index(ast, 'definition', 'identifier') | ||
var definitionsByUrl = new Index(ast, 'definition', 'url') | ||
var definitions = [] | ||
var hosts = Object.create(null) | ||
|
||
postorder(ast) | ||
|
||
push.apply(ast.children, definitions) | ||
|
||
function postorder(node) { | ||
var nodes | ||
|
||
module.exports = function (opts) { | ||
opts = opts || {}; | ||
opts.id = (opts.id | ||
? (Array.isArray(opts.id) | ||
? opts.id.map(String) | ||
: [String(opts.id)]) | ||
: []); | ||
|
||
return function (ast) { | ||
var definitionsById = Index(ast, 'definition', function (definition) { | ||
return definition.identifier.toLowerCase(); | ||
}); | ||
var definitionsByLink = Index(ast, 'definition', 'url'); | ||
var newDefinitions = []; | ||
var hostCount = Object.create(null); | ||
|
||
(function postorder (node) { | ||
if (node.children) { | ||
node.children = concat.apply([], node.children.map(postorder)); | ||
node.children = concat.apply([], node.children.map(postorder)) | ||
} | ||
return forNode.apply(null, arguments) || node; | ||
}(ast)); | ||
|
||
[].push.apply(ast.children, newDefinitions); | ||
|
||
function forNode (node) { | ||
if (node.type == 'definition' || node.type == 'heading') { | ||
var nodes = newDefinitions.concat(node); | ||
newDefinitions.length = 0; | ||
return nodes; | ||
if (node.type === 'definition' || node.type === 'heading') { | ||
nodes = definitions.concat(node) | ||
definitions = [] | ||
return nodes | ||
} | ||
|
||
if (node.type !== 'link' && node.type !== 'image') return; | ||
if (node.type === 'link' || node.type === 'image') { | ||
node.type += 'Reference' | ||
node.referenceType = 'full' | ||
node.identifier = identifier(node.url, node.title) | ||
|
||
node.type += 'Reference'; | ||
node.referenceType = 'full'; | ||
node.identifier = identifier(node.url, node.title); | ||
delete node.url | ||
delete node.title | ||
} | ||
|
||
delete node.url; | ||
delete node.title; | ||
return node | ||
} | ||
|
||
function identifier (link, title) { | ||
var identifier; | ||
|
||
var found = definitionsByLink.get(link).some(function (def) { | ||
if (def.title == title) { | ||
identifier = def.identifier; | ||
return true; | ||
} | ||
}); | ||
function identifier(link, title) { | ||
var identifier = null | ||
var found = definitionsByUrl.get(link).some(some) | ||
var host | ||
var definition | ||
|
||
if (found) { | ||
return identifier; | ||
return identifier | ||
} | ||
|
||
if (!(identifier = opts.id.shift())) { | ||
var host = urlHost(link); | ||
hostCount[host] |= 0; | ||
identifier = ids.shift() | ||
|
||
if (!identifier) { | ||
host = urlHost(link) | ||
hosts[host] |= 0 | ||
|
||
do { | ||
identifier = (host ? host + '-' : '') + ++hostCount[host]; | ||
} while (definitionsById.get(identifier).length); | ||
identifier = (host ? host + '-' : '') + ++hosts[host] | ||
} while (definitionsById.get(identifier).length) | ||
} | ||
|
||
var newDefinition = { | ||
definition = { | ||
type: 'definition', | ||
identifier: identifier, | ||
title: title, | ||
url: link | ||
}; | ||
} | ||
|
||
newDefinitions.push(newDefinition); | ||
definitionsById.add(newDefinition); | ||
definitionsByLink.add(newDefinition); | ||
definitions.push(definition) | ||
definitionsById.add(definition) | ||
definitionsByUrl.add(definition) | ||
|
||
return identifier; | ||
} | ||
}; | ||
}; | ||
return identifier | ||
|
||
function some(def) { | ||
if (def.title === title) { | ||
identifier = def.identifier | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
} | ||
} | ||
} | ||
|
||
function urlHost (link) { | ||
var host = url.parse(link).host; | ||
return host ? path.parse(host).name : ''; | ||
function urlHost(link) { | ||
var host = url.parse(link).host | ||
return host ? path.parse(host).name : '' | ||
} |
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
Oops, something went wrong.