-
-
Notifications
You must be signed in to change notification settings - Fork 478
/
Copy pathtransform.js
172 lines (131 loc) · 5.07 KB
/
transform.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
'use strict';
var _ = require('underscore')
, anchor = require('anchor-markdown-header')
, updateSection = require('update-section')
, getHtmlHeaders = require('./get-html-headers')
, md = require('@textlint/markdown-to-ast');
var start = '<!-- START doctoc generated TOC please keep comment here to allow auto update -->\n' +
'<!-- DON\'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->'
, end = '<!-- END doctoc generated TOC please keep comment here to allow auto update -->'
, skipTag = '<!-- DOCTOC SKIP -->';
function matchesStart(line) {
return (/<!-- START doctoc /).test(line);
}
function matchesEnd(line) {
return (/<!-- END doctoc /).test(line);
}
function notNull(x) { return x !== null; }
function addAnchor(mode, header) {
header.anchor = anchor(header.name, mode, header.instance);
return header;
}
function isString(y) {
return typeof y === 'string';
}
function getMarkdownHeaders (lines, maxHeaderLevel) {
function extractText (header) {
return header.children
.map(function (x) {
if (x.type === md.Syntax.Link) {
return extractText(x);
}
else if (x.type === md.Syntax.Image) {
// Images (at least on GitHub, untested elsewhere) are given a hyphen
// in the slug. We can achieve this behavior by adding an '*' to the
// TOC entry. Think of it as a "magic char" that represents the iamge.
return '*';
}
else {
return x.raw;
}
})
.join('')
}
return md.parse(lines.join('\n')).children
.filter(function (x) {
return x.type === md.Syntax.Header;
})
.map(function (x) {
return !maxHeaderLevel || x.depth <= maxHeaderLevel
? { rank : x.depth
, name : extractText(x)
, line : x.loc.start.line
}
: null;
})
.filter(notNull)
}
function countHeaders (headers) {
var instances = {};
for (var i = 0; i < headers.length; i++) {
var header = headers[i];
var name = header.name;
if (Object.prototype.hasOwnProperty.call(instances, name)) {
// `instances.hasOwnProperty(name)` fails when there’s an instance named "hasOwnProperty".
instances[name]++;
} else {
instances[name] = 0;
}
header.instance = instances[name];
}
return headers;
}
function getLinesToToc (lines, currentToc, info, processAll) {
if (processAll || !currentToc) return lines;
var tocableStart = 0;
// when updating an existing toc, we only take the headers into account
// that are below the existing toc
if (info.hasEnd) tocableStart = info.endIdx + 1;
return lines.slice(tocableStart);
}
// Use document context as well as command line args to infer the title
function determineTitle(title, notitle, lines, info) {
var defaultTitle = '**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*';
if (notitle) return '';
if (title) return title;
return info.hasStart ? lines[info.startIdx + 2] : defaultTitle;
}
exports = module.exports = function transform(content, mode, maxHeaderLevel, title, notitle, entryPrefix, processAll, updateOnly) {
if (content.indexOf(skipTag) !== -1) return { transformed: false };
mode = mode || 'github.com';
entryPrefix = entryPrefix || '-';
// only limit *HTML* headings by default
var maxHeaderLevelHtml = maxHeaderLevel || 4;
var lines = content.split('\n')
, info = updateSection.parse(lines, matchesStart, matchesEnd)
if (!info.hasStart && updateOnly) {
return { transformed: false };
}
var inferredTitle = determineTitle(title, notitle, lines, info);
var titleSeparator = inferredTitle ? '\n\n' : '\n';
var currentToc = info.hasStart && lines.slice(info.startIdx, info.endIdx + 1).join('\n')
, linesToToc = getLinesToToc(lines, currentToc, info, processAll);
var headers = getMarkdownHeaders(linesToToc, maxHeaderLevel)
.concat(getHtmlHeaders(linesToToc, maxHeaderLevelHtml))
headers.sort(function (a, b) {
return a.line - b.line;
});
var allHeaders = countHeaders(headers)
, lowestRank = _(allHeaders).chain().pluck('rank').min().value()
, linkedHeaders = _(allHeaders).map(addAnchor.bind(null, mode));
if (linkedHeaders.length === 0) return { transformed: false };
// 4 spaces required for proper indention on Bitbucket and GitLab
var indentation = (mode === 'bitbucket.org' || mode === 'gitlab.com') ? ' ' : ' ';
var toc =
inferredTitle
+ titleSeparator
+ linkedHeaders
.map(function (x) {
var indent = _(_.range(x.rank - lowestRank))
.reduce(function (acc, x) { return acc + indentation; }, '');
return indent + entryPrefix + ' ' + x.anchor;
})
.join('\n')
+ '\n';
var wrappedToc = start + '\n' + toc + '\n' + end;
if (currentToc === toc) return { transformed: false };
var data = updateSection(lines.join('\n'), wrappedToc, matchesStart, matchesEnd, true);
return { transformed : true, data : data, toc: toc, wrappedToc: wrappedToc };
};
exports.start = start;
exports.end = end;