forked from jonschlinkert/markdown-toc
-
Notifications
You must be signed in to change notification settings - Fork 49
/
index.js
124 lines (102 loc) · 2.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'use strict';
/**
* Module dependencies
*/
const utils = require('./lib/utils');
const fs = require('fs');
const path = require('path');
const slash = require('slash');
var counter = 1;
/**
* expose `toc`
*/
module.exports = toc;
/**
* Load `generate` as a remarkable plugin and
* expose the `toc` function.
*
* @param {String} `str` String of markdown
* @param {Object} `options`
* @return {String} Markdown-formatted table of contents
*/
function toc(str, options) {
return new utils.Remarkable()
.use(generate(options))
.render(str);
}
/**
* Expose `insert` method
*/
toc.insertAdrToc = require('./lib/insert').insertAdrToc;
/**
* extract index number or date from either filename or front matter
*/
function getIndex(basename, data) {
const numb = basename.match(/^[\d-_\\\/]*\d/m);
if (numb !== null && numb !== undefined) {
return numb[0];
}
if (data.index) {
return '' + data.index;
}
if (data.date) {
let date = data.date;
if (typeof(date) == 'object') {
date = date.getFullYear() + "-"
+ ("0" + (date.getMonth() + 1)).substr(-2) + "-"
+ ("0" + date.getDate()).substr(-2)
}
return date;
}
return '' + (counter++);
}
/**
* Generate a markdown table of contents. This is the
* function that does all of the main work with Remarkable.
*
* This is the core implementation. Here, the file contents (docs/adr/dddd-*.md) are parsed and transformed into markdown.
*/
function generate(options) {
return function (md) {
md.renderer.render = function (tokens) {
const res = {
content: ''
};
tokens = tokens.filter(t => !!t.content);
for (const token of tokens) {
if (!options.dir) {
return {
content: ''
};
}
let contentPath = `${options.dir}/${token.content}`
let content = fs.readFileSync(contentPath).toString();
let tokenPath = slash(
path.relative(options.tocDir, contentPath)
);
let data = {};
// does the file have front-matter?
if (/^---/.test(content)) {
// extract it temporarily so the syntax
// doesn't get mistaken for a heading
let obj = utils.matter(content);
data = obj.data;
content = obj.content;
}
let index = getIndex(path.parse(token.content).base, data);
const newline = utils.determineNewline(content);
let title = content.split(newline)[0].substr(2);
console.log("title before decimal removal: ", title);
title = title.replace(/^\d+\. /, '');
console.log("title after decimal removal: ", title);
res.content += `${options.bulletChar} [ADR-${index.trim()}](${options.pathPrefix}${tokenPath}) - ${title + options.newline}`
}
res.content = res.content.trim();
return res;
};
};
}
/**
* Expose utils
*/
toc.slugify = utils.slugify;