WIP - add extractor, generate snippet_data
This commit is contained in:
66
node_modules/mdast-util-toc/lib/contents.js
generated
vendored
Normal file
66
node_modules/mdast-util-toc/lib/contents.js
generated
vendored
Normal file
@ -0,0 +1,66 @@
|
||||
/**
|
||||
* @author Titus Wormer
|
||||
* @copyright 2015 Titus Wormer
|
||||
* @license MIT
|
||||
* @module mdast:toc
|
||||
* @fileoverview Generate a Table of Contents (TOC) from a given Markdown file.
|
||||
*/
|
||||
|
||||
/* Expose. */
|
||||
module.exports = contents;
|
||||
|
||||
/* Dependencies */
|
||||
var list = require('./list');
|
||||
var insert = require('./insert');
|
||||
|
||||
/**
|
||||
* Transform a list of heading objects to a markdown list.
|
||||
*
|
||||
* @param {Array.<Object>} map - Heading-map to insert.
|
||||
* @param {boolean?} [tight] - Prefer tight list-items.
|
||||
* @return {Object} - List node.
|
||||
*/
|
||||
function contents(map, tight) {
|
||||
var minDepth = Infinity;
|
||||
var index = -1;
|
||||
var length = map.length;
|
||||
var table;
|
||||
|
||||
/*
|
||||
* Find minimum depth.
|
||||
*/
|
||||
|
||||
while (++index < length) {
|
||||
if (map[index].depth < minDepth) {
|
||||
minDepth = map[index].depth;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Normalize depth.
|
||||
*/
|
||||
|
||||
index = -1;
|
||||
|
||||
while (++index < length) {
|
||||
map[index].depth -= minDepth - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Construct the main list.
|
||||
*/
|
||||
|
||||
table = list();
|
||||
|
||||
/*
|
||||
* Add TOC to list.
|
||||
*/
|
||||
|
||||
index = -1;
|
||||
|
||||
while (++index < length) {
|
||||
insert(map[index], table, tight);
|
||||
}
|
||||
|
||||
return table;
|
||||
}
|
||||
39
node_modules/mdast-util-toc/lib/index.js
generated
vendored
Normal file
39
node_modules/mdast-util-toc/lib/index.js
generated
vendored
Normal file
@ -0,0 +1,39 @@
|
||||
/**
|
||||
* @author Titus Wormer
|
||||
* @copyright 2015 Titus Wormer
|
||||
* @license MIT
|
||||
* @module mdast:toc
|
||||
* @fileoverview Generate a Table of Contents (TOC) from a given Markdown file.
|
||||
*/
|
||||
|
||||
/* Expose. */
|
||||
module.exports = toc;
|
||||
|
||||
/* Dependencies */
|
||||
var toExpression = require('./to-expression');
|
||||
var search = require('./search');
|
||||
var contents = require('./contents');
|
||||
|
||||
/**
|
||||
* Get a TOC representation of `node`.
|
||||
*
|
||||
* @param {Mdast} node - MDAST.
|
||||
* @param {Object} options - Configuration.
|
||||
* @return {Array} - TOC Markdown.
|
||||
*/
|
||||
function toc(node, options) {
|
||||
var settings = options || {};
|
||||
var heading = settings.heading ? toExpression(settings.heading) : null;
|
||||
var result = search(node, heading, settings.maxDepth || 6);
|
||||
var map = result.map;
|
||||
|
||||
result.map = map.length ? contents(map, settings.tight) : null;
|
||||
|
||||
/* No given heading */
|
||||
if (!heading) {
|
||||
result.index = null;
|
||||
result.endIndex = null;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
108
node_modules/mdast-util-toc/lib/insert.js
generated
vendored
Normal file
108
node_modules/mdast-util-toc/lib/insert.js
generated
vendored
Normal file
@ -0,0 +1,108 @@
|
||||
/**
|
||||
* @author Titus Wormer
|
||||
* @copyright 2015 Titus Wormer
|
||||
* @license MIT
|
||||
* @module mdast:toc
|
||||
* @fileoverview Generate a Table of Contents (TOC) from a given Markdown file.
|
||||
*/
|
||||
|
||||
/* Expose. */
|
||||
module.exports = insert;
|
||||
|
||||
/* Dependencies */
|
||||
var listItem = require('./list-item');
|
||||
var list = require('./list');
|
||||
|
||||
/* Constants */
|
||||
var LIST = 'list';
|
||||
var LIST_ITEM = 'listItem';
|
||||
var PARAGRAPH = 'paragraph';
|
||||
var LINK = 'link';
|
||||
var TEXT = 'text';
|
||||
|
||||
/**
|
||||
* Insert a `node` into a `parent`.
|
||||
*
|
||||
* @param {Object} node - `node` to insert.
|
||||
* @param {Object} parent - Parent of `node`.
|
||||
* @param {boolean?} [tight] - Prefer tight list-items.
|
||||
* @return {undefined}
|
||||
*/
|
||||
function insert(node, parent, tight) {
|
||||
var children = parent.children;
|
||||
var length = children.length;
|
||||
var last = children[length - 1];
|
||||
var isLoose = false;
|
||||
var index;
|
||||
var item;
|
||||
|
||||
if (node.depth === 1) {
|
||||
item = listItem();
|
||||
|
||||
item.children.push({
|
||||
type: PARAGRAPH,
|
||||
children: [
|
||||
{
|
||||
type: LINK,
|
||||
title: null,
|
||||
url: '#' + node.id,
|
||||
children: [
|
||||
{
|
||||
type: TEXT,
|
||||
value: node.value
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
children.push(item);
|
||||
} else if (last && last.type === LIST_ITEM) {
|
||||
insert(node, last, tight);
|
||||
} else if (last && last.type === LIST) {
|
||||
node.depth--;
|
||||
|
||||
insert(node, last, tight);
|
||||
} else if (parent.type === LIST) {
|
||||
item = listItem();
|
||||
|
||||
insert(node, item, tight);
|
||||
|
||||
children.push(item);
|
||||
} else {
|
||||
item = list();
|
||||
node.depth--;
|
||||
|
||||
insert(node, item, tight);
|
||||
|
||||
children.push(item);
|
||||
}
|
||||
|
||||
/*
|
||||
* Properly style list-items with new lines.
|
||||
*/
|
||||
|
||||
if (parent.type === LIST_ITEM) {
|
||||
parent.loose = tight ? false : children.length > 1;
|
||||
} else {
|
||||
if (tight) {
|
||||
isLoose = false;
|
||||
} else {
|
||||
index = -1;
|
||||
|
||||
while (++index < length) {
|
||||
if (children[index].loose) {
|
||||
isLoose = true;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
index = -1;
|
||||
|
||||
while (++index < length) {
|
||||
children[index].loose = isLoose;
|
||||
}
|
||||
}
|
||||
}
|
||||
24
node_modules/mdast-util-toc/lib/is-closing-heading.js
generated
vendored
Normal file
24
node_modules/mdast-util-toc/lib/is-closing-heading.js
generated
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* @author Titus Wormer
|
||||
* @copyright 2015 Titus Wormer
|
||||
* @license MIT
|
||||
* @module mdast:toc
|
||||
* @fileoverview Generate a Table of Contents (TOC) from a given Markdown file.
|
||||
*/
|
||||
|
||||
/* Expose. */
|
||||
module.exports = isClosingHeading;
|
||||
|
||||
/* Constants. */
|
||||
var HEADING = 'heading';
|
||||
|
||||
/**
|
||||
* Check if `node` is the next heading.
|
||||
*
|
||||
* @param {Node} node - Node to check.
|
||||
* @param {number} depth - Depth of opening heading.
|
||||
* @return {boolean} - Whether znode is a closing heading.
|
||||
*/
|
||||
function isClosingHeading(node, depth) {
|
||||
return depth && node && node.type === HEADING && node.depth <= depth;
|
||||
}
|
||||
33
node_modules/mdast-util-toc/lib/is-opening-heading.js
generated
vendored
Normal file
33
node_modules/mdast-util-toc/lib/is-opening-heading.js
generated
vendored
Normal file
@ -0,0 +1,33 @@
|
||||
/**
|
||||
* @author Titus Wormer
|
||||
* @copyright 2015 Titus Wormer
|
||||
* @license MIT
|
||||
* @module mdast:toc
|
||||
* @fileoverview Generate a Table of Contents (TOC) from a given Markdown file.
|
||||
*/
|
||||
|
||||
/* Expose. */
|
||||
module.exports = isOpeningHeading;
|
||||
|
||||
/* Dependencies */
|
||||
var toString = require('mdast-util-to-string');
|
||||
|
||||
/* Constants. */
|
||||
var HEADING = 'heading';
|
||||
|
||||
/**
|
||||
* Check if `node` is the main heading.
|
||||
*
|
||||
* @param {Node} node - Node to check.
|
||||
* @param {number} depth - Depth to check.
|
||||
* @param {RegExp} expression - Expression to check.
|
||||
* @return {boolean} - Whether `node` is a main heading.
|
||||
*/
|
||||
function isOpeningHeading(node, depth, expression) {
|
||||
return (
|
||||
depth === null &&
|
||||
node &&
|
||||
node.type === HEADING &&
|
||||
expression.test(toString(node))
|
||||
);
|
||||
}
|
||||
26
node_modules/mdast-util-toc/lib/list-item.js
generated
vendored
Normal file
26
node_modules/mdast-util-toc/lib/list-item.js
generated
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
/**
|
||||
* @author Titus Wormer
|
||||
* @copyright 2015 Titus Wormer
|
||||
* @license MIT
|
||||
* @module mdast:toc
|
||||
* @fileoverview Generate a Table of Contents (TOC) from a given Markdown file.
|
||||
*/
|
||||
|
||||
/* Expose. */
|
||||
module.exports = listItem;
|
||||
|
||||
/* Constants */
|
||||
var LIST_ITEM = 'listItem';
|
||||
|
||||
/**
|
||||
* Create a list item.
|
||||
*
|
||||
* @return {Object} - List-item node.
|
||||
*/
|
||||
function listItem() {
|
||||
return {
|
||||
type: LIST_ITEM,
|
||||
loose: false,
|
||||
children: []
|
||||
};
|
||||
}
|
||||
26
node_modules/mdast-util-toc/lib/list.js
generated
vendored
Normal file
26
node_modules/mdast-util-toc/lib/list.js
generated
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
/**
|
||||
* @author Titus Wormer
|
||||
* @copyright 2015 Titus Wormer
|
||||
* @license MIT
|
||||
* @module mdast:toc
|
||||
* @fileoverview Generate a Table of Contents (TOC) from a given Markdown file.
|
||||
*/
|
||||
|
||||
/* Expose. */
|
||||
module.exports = list;
|
||||
|
||||
/* Constants */
|
||||
var LIST = 'list';
|
||||
|
||||
/**
|
||||
* Create a list.
|
||||
*
|
||||
* @return {Object} - List node.
|
||||
*/
|
||||
function list() {
|
||||
return {
|
||||
type: LIST,
|
||||
ordered: false,
|
||||
children: []
|
||||
};
|
||||
}
|
||||
97
node_modules/mdast-util-toc/lib/search.js
generated
vendored
Normal file
97
node_modules/mdast-util-toc/lib/search.js
generated
vendored
Normal file
@ -0,0 +1,97 @@
|
||||
/**
|
||||
* @author Titus Wormer
|
||||
* @copyright 2015 Titus Wormer
|
||||
* @license MIT
|
||||
* @module mdast:toc
|
||||
* @fileoverview Generate a Table of Contents (TOC) from a given Markdown file.
|
||||
*/
|
||||
|
||||
/* Expose. */
|
||||
module.exports = search;
|
||||
|
||||
/* Dependencies */
|
||||
var toString = require('mdast-util-to-string');
|
||||
var visit = require('unist-util-visit');
|
||||
var slugs = require('github-slugger')();
|
||||
var isClosingHeading = require('./is-closing-heading');
|
||||
var isOpeningHeading = require('./is-opening-heading');
|
||||
|
||||
/* Constants. */
|
||||
var HEADING = 'heading';
|
||||
|
||||
/**
|
||||
* Search a node for a location.
|
||||
*
|
||||
* @param {Node} root - Parent to search in.
|
||||
* @param {RegExp} expression - Heading-content to search
|
||||
* for.
|
||||
* @param {number} maxDepth - Maximum-depth to include.
|
||||
* @return {Object} - Results.
|
||||
*/
|
||||
function search(root, expression, maxDepth) {
|
||||
var length = root.children.length;
|
||||
var depth = null;
|
||||
var lookingForToc = expression !== null;
|
||||
var map = [];
|
||||
var headingIndex;
|
||||
var closingIndex;
|
||||
|
||||
if (!lookingForToc) {
|
||||
headingIndex = -1;
|
||||
}
|
||||
|
||||
slugs.reset();
|
||||
|
||||
/*
|
||||
* Visit all headings in `root`.
|
||||
* We `slug` all headings (to account for duplicates),
|
||||
* but only create a TOC from top-level headings.
|
||||
*/
|
||||
|
||||
visit(root, HEADING, function(child, index, parent) {
|
||||
var value = toString(child);
|
||||
var id =
|
||||
child.data && child.data.hProperties && child.data.hProperties.id;
|
||||
id = slugs.slug(id || value);
|
||||
|
||||
if (parent !== root) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (lookingForToc) {
|
||||
if (isClosingHeading(child, depth)) {
|
||||
closingIndex = index;
|
||||
lookingForToc = false;
|
||||
}
|
||||
|
||||
if (isOpeningHeading(child, depth, expression)) {
|
||||
headingIndex = index + 1;
|
||||
depth = child.depth;
|
||||
}
|
||||
}
|
||||
|
||||
if (!lookingForToc && value && child.depth <= maxDepth) {
|
||||
map.push({
|
||||
depth: child.depth,
|
||||
value: value,
|
||||
id: id
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
if (headingIndex && !closingIndex) {
|
||||
closingIndex = length + 1;
|
||||
}
|
||||
|
||||
if (headingIndex === undefined) {
|
||||
headingIndex = -1;
|
||||
closingIndex = -1;
|
||||
map = [];
|
||||
}
|
||||
|
||||
return {
|
||||
index: headingIndex,
|
||||
endIndex: closingIndex,
|
||||
map: map
|
||||
};
|
||||
}
|
||||
20
node_modules/mdast-util-toc/lib/to-expression.js
generated
vendored
Normal file
20
node_modules/mdast-util-toc/lib/to-expression.js
generated
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
/**
|
||||
* @author Titus Wormer
|
||||
* @copyright 2015 Titus Wormer
|
||||
* @license MIT
|
||||
* @module mdast:toc
|
||||
* @fileoverview Generate a Table of Contents (TOC) from a given Markdown file.
|
||||
*/
|
||||
|
||||
/* Expose. */
|
||||
module.exports = toExpression;
|
||||
|
||||
/**
|
||||
* Transform a string into an applicable expression.
|
||||
*
|
||||
* @param {string} value - Content to expressionise.
|
||||
* @return {RegExp} - Expression from `value`.
|
||||
*/
|
||||
function toExpression(value) {
|
||||
return new RegExp('^(' + value + ')$', 'i');
|
||||
}
|
||||
Reference in New Issue
Block a user