-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
184 lines (159 loc) · 5.19 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
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
173
174
175
176
177
178
179
180
181
182
183
'use strict';
var url = require('url');
var util = require('util');
var md = require('markdown-it')({linkify: true, html: true});
//.use(require('markdown-it-lazy-headers'));
var cheerio = require('cheerio');
var exampleValidator = require('./parseExamples.js');
function indexAnyOf(s,a) {
for (let comp of a) {
let tmp = s.indexOf(comp);
if (tmp >= 0) return tmp;
}
return -1;
}
function gfmLink(text) {
text = text.trim().toLowerCase();
text = text.split("'").join('');
text = text.split('"').join('');
text = text.split('.').join('');
text = text.split('`').join('');
text = text.split(':').join('');
text = text.split('/').join('');
text = text.split('<').join('');
text = text.split('>').join('');
text = text.split('<').join('');
text = text.split('>').join('');
text = text.split('(').join('');
text = text.split(')').join('');
text = text.split(' ').join('-');
return text.toLowerCase();
}
function define(anchors,name,auto,suffix) {
var oname = name;
if (suffix) name = name+'-'+suffix;
var anchor = anchors.find(function(e,i,a){
return e.name == name;
});
if (anchor) {
if (auto) {
define(anchors,oname,auto,suffix ? suffix+1 : 1);
}
else {
anchor.defined++;
}
}
else {
anchor = {
name: name,
defined: 1,
seen: 0,
auto: auto
};
anchors.push(anchor);
}
}
function validate(s, options) {
const result = {};
if (options.source) result.source = options.source;
result.imagesWithMissingAlt = 0;
if (options.warnings) {
result.tooManyBackticks = 0;
const lines = s.split('\r').join('').split('\n');
for (let line of lines) {
if (line.startsWith('````')) {
result.tooManyBackticks++;
}
}
}
var html = md.render(s);
var $ = cheerio.load(html);
var anchors = [];
$("a").each(function () {
var name = $(this).attr('id') || $(this).attr('name');
if (name) {
define(anchors,name,false);
}
});
// GFM auto-links
for (var heading of ["h1","h2","h3","h4","h5","h6"]) {
var elements = $(heading).each(function() {
var text = gfmLink($(this).text());
define(anchors,text,true);
});
}
$("a").each(function () {
var href = $(this).attr('href');
if (href) {
var local = true;
var u = url.parse(href);
if (u.protocol || (u.path && u.path.startsWith('/')) || href.startsWith('./') || href.startsWith('../')) local = false;
if (indexAnyOf(href,['.md','.html','.json','.yaml','.yml'])>=0) local = false; // FIXME buggy for relative links to files like `LICENSE`
if (local) {
var localRefNoHash = !href.startsWith('#');
var ptr = href.replace('#','');
var anchor = anchors.find(function(e,i,a){
// fragment names are case-sensitive: https://www.w3.org/MarkUp/html-spec/html-spec_7.html#SEC7.4
return e.name == ptr;
});
if (anchor) {
anchor.seen++;
if (localRefNoHash) anchor.localRefNoHash = true;
}
else {
anchor = {
name: ptr,
defined: 0,
seen: 1,
localRefNoHash: localRefNoHash
};
anchors.push(anchor);
}
if (!$(this).text()) {
anchor.emptyText = anchor.emptyText ? anchor.emptyText++ : 1;
}
}
}
});
$("img").each(function() {
if (!$(this).attr('alt')) {
result.imagesWithMissingAlt++;
}
});
result.missingAnchors = anchors.filter(function(e,i,a){
return (!e.defined && e.seen && indexAnyOf(e.name,['.md','.html','.json','.yaml','.yml'])<0);
});
result.duplicatedAnchors = anchors.filter(function(e,i,a){
return (e.defined>1);
});
result.anchorsWithHash = anchors.filter(function(e,i,a){
return (e.name.startsWith('#'));
});
result.anchorsWithEmptyText = anchors.filter(function(e,i,a){
return e.emptyText;
});
result.localRefNoHash = anchors.filter(function(e,i,a){
return e.localRefNoHash;
});
result.nonParsingExamples = exampleValidator.parseExamples(s,options);
if (options.warnings) {
result.codeBlocksWithNoLanguage = 0;
$("pre > code").each(function(){
var classes = ($(this).attr('class')||'').split(' ');
var lang = classes.find(function(e,i,a){
return e.startsWith('language');
});
if (!lang) result.codeBlocksWithNoLanguage++;
});
result.anchorsWithNoLinks = anchors.filter(function(e,i,a){
return (e.defined && !e.seen && !e.auto);
});
}
if (options.save) {
options.html = html;
}
return result;
}
module.exports = {
validate : validate
};