-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathutil.js
More file actions
156 lines (135 loc) · 4.44 KB
/
util.js
File metadata and controls
156 lines (135 loc) · 4.44 KB
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
import marked from 'marked';
import escape from 'escape-html';
/**
* shorten description.
* e.g. ``this is JavaScript. this is Java.`` => ``this is JavaScript.``.
*
* @param {DocObject} doc - target doc object.
* @param {boolean} [asMarkdown=false] - is true, test as markdown and convert to html.
* @returns {string} shorten description.
* @todo shorten before process markdown.
*/
export function shorten(doc, asMarkdown = false) {
if (!doc) return '';
if (doc.summary) return doc.summary;
const desc = doc.descriptionRaw;
if (!desc) return '';
let len = desc.length;
let inSQuote = false;
let inWQuote = false;
let inCode = false;
for (let i = 0; i < desc.length; i++) {
const char1 = desc.charAt(i);
const char2 = desc.charAt(i + 1);
const char4 = desc.substr(i, 6);
const char5 = desc.substr(i, 7);
if (char1 === '\'') inSQuote = !inSQuote;
else if (char1 === '"') inWQuote = !inWQuote;
else if (char4 === '<code>') inCode = true;
else if (char5 === '</code>') inCode = false;
if (inSQuote || inCode || inWQuote) continue;
if (char1 === '.') {
if (char2 === ' ' || char2 === '\n' || char2 === '<') {
len = i + 1;
break;
}
} else if (char1 === '\n' && char2 === '\n') {
len = i + 1;
break;
}
}
let result = desc.substr(0, len);
if (asMarkdown) {
result = markdown(result);
}
return result;
}
/**
* convert markdown text to html.
* @param {string} text - markdown text.
* @param {boolean} [breaks=false] if true, break line. FYI gfm is not breaks.
* @return {string} html.
*/
export function markdown(text, breaks = false) {
// original render does not support multi-byte anchor
const renderer = new marked.Renderer();
renderer.heading = function (text, level) {
const id = escapeURLHash(text);
return `<h${level} id=${id}>${text}</h${level}>`;
};
const availableTags = ['span', 'a', 'p', 'div', 'img', 'h1', 'h2', 'h3', 'h4', 'h5', 'br', 'hr', 'li', 'ul', 'ol', 'code', 'pre', 'details', 'summary', 'kbd'];
const availableAttributes = ['src', 'href', 'title', 'class', 'id', 'name', 'width', 'height', 'target'];
const compiled = marked(text, {
renderer: renderer,
gfm: true,
tables: true,
breaks: breaks,
sanitize: true,
sanitizer: (tag) =>{
if (tag.match(/<!--.*-->/)) {
return tag;
}
const tagName = tag.match(/^<\/?(\w+)/)[1];
if (!availableTags.includes(tagName)) {
return escape(tag);
}
const sanitizedTag = tag.replace(/([\w\-]+)=(["'].*?["'])/g, (_, attr, val)=>{
if (!availableAttributes.includes(attr)) return '';
/* eslint-disable no-script-url */
if (val.indexOf('javascript:') !== -1) return '';
return `${attr}=${val}`;
});
return sanitizedTag;
},
highlight: function(code) {
// return `<pre class="source-code"><code class="prettyprint">${escape(code)}</code></pre>`;
return `<code class="source-code prettyprint">${escape(code)}</code>`;
}
});
return compiled;
}
/**
* get UTC date string.
* @param {Date} date - target date object.
* @returns {string} UTC date string(yyyy-mm-dd hh:mm:ss)
*/
export function dateForUTC(date) {
function pad(num, len) {
const count = Math.max(0, len - `${num}`.length);
return '0'.repeat(count) + num;
}
const year = date.getUTCFullYear();
const month = pad(date.getUTCMonth() + 1, 2);
const day = pad(date.getUTCDay() + 1, 2);
const hours = pad(date.getUTCHours(), 2);
const minutes = pad(date.getUTCMinutes(), 2);
const seconds = pad(date.getUTCSeconds(), 2);
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds} (UTC)`;
}
/**
* parse ``@example`` value.
* ``@example`` value can have ``<caption>`` tag.
*
* @param {string} example - target example value.
* @returns {{body: string, caption: string}} parsed example value.
*/
export function parseExample(example) {
let body = example;
let caption = '';
/* eslint-disable no-control-regex */
const regexp = new RegExp('^<caption>(.*?)</caption>\n');
const matched = example.match(regexp);
if (matched) {
body = example.replace(regexp, '');
caption = matched[1].trim();
}
return {body, caption};
}
/**
* escape URL hash.
* @param {string} hash - URL hash for HTML a tag and id tag
* @returns {string} escaped URL hash
*/
export function escapeURLHash(hash) {
return hash.toLowerCase().replace(/[~!@#$%^&*()_+=\[\]\\{}|;':"<>?,.\/ ]/g, '-');
}