Created
March 5, 2014 19:20
-
-
Save Raynos/9374532 to your computer and use it in GitHub Desktop.
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
var marked = require('marked'); | |
var findIndex = require('lodash.findindex'); | |
var doctoc = require('doctoc/lib/transform'); | |
var fs = require('fs'); | |
var path = require('path'); | |
/* | |
WIP to automate the inclusion of various documentation into | |
the README. | |
This requires having a way to turn `marked` lexed tokens back | |
into raw text. | |
For now this is un-used | |
*/ | |
var cwd = process.cwd(); | |
var README = fs.readFileSync(path.join(cwd, 'README.md'), 'utf8'); | |
var tokens = marked.lexer(README); | |
function byHeadings(tokens) { | |
return tokens.reduce(function (acc, token) { | |
if (token.type === 'heading') { | |
var header = { | |
header: token, | |
tokens: [] | |
}; | |
acc.push(header); | |
return acc; | |
} | |
var last = acc[acc.length - 1]; | |
last.tokens.push(token); | |
return acc; | |
}, []); | |
} | |
function toTokens(headings) { | |
return headings.reduce(function (acc, header) { | |
acc.push(header.header); | |
acc = acc.concat(header.tokens); | |
return acc; | |
}, []); | |
} | |
function rewriteDocToc(header) { | |
var tokens = header.tokens; | |
var index = findIndex(tokens, function (token) { | |
return token.type === 'paragraph' && | |
token.text.indexOf('generated with [DocToc]') !== -1; | |
}); | |
var newTokens = tokens.slice(0, index); | |
var toc = marked.lexer(doctoc(README).toc); | |
newTokens = newTokens.concat(toc); | |
return { header: header.header, tokens: newTokens }; | |
} | |
var headings = byHeadings(tokens); | |
headings[0] = rewriteDocToc(headings[0]); | |
var newTokens = toTokens(headings); | |
var types = newTokens.reduce(function (acc, token) { | |
if (token.type === undefined) { | |
console.log('token', token); | |
} | |
acc[token.type] = true; | |
return acc; | |
}, {}); | |
console.log('types', types); | |
// newTokens.links = tokens.links; | |
// var text = marked.parser(newTokens); | |
// console.log(text); | |
// console.log('headings', tokens); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment