-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannounce.js
More file actions
203 lines (164 loc) · 5.85 KB
/
Copy pathannounce.js
File metadata and controls
203 lines (164 loc) · 5.85 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// shared announcement markup renderer, used by /home and /admin
(function () {
"use strict";
// section for the inline marks
var INLINE = [
{ re: /\*\*([^*]+)\*\*/, tag: "strong" },
{ re: /__([^_]+)__/, tag: "u" },
{ re: /~~([^~]+)~~/, tag: "s" },
{ re: /`([^`]+)`/, tag: "code", cls: "annCode" },
{ re: /\*([^*\n]+)\*/, tag: "em" },
{ re: /_([^_\n]+)_/, tag: "em" }
];
var LINK = /\[([^\]\n]+)\]\(([^)\s]+)\)/;
function safeHref(url) {
return /^https?:\/\//i.test(url) ? url : null;
}
// the innermost match wins, so **bold** is never eaten by *italic*
function firstMark(text) {
var best = null;
var link = LINK.exec(text);
if (link) best = { at: link.index, len: link[0].length, label: link[1], href: link[2], link: true };
for (var i = 0; i < INLINE.length; i++) {
var hit = INLINE[i].re.exec(text);
if (!hit) continue;
if (best && hit.index >= best.at) continue;
best = { at: hit.index, len: hit[0].length, label: hit[1],
tag: INLINE[i].tag, cls: INLINE[i].cls };
}
return best;
}
function inline(target, text) {
var rest = String(text);
while (rest) {
var mark = firstMark(rest);
if (!mark) break;
if (mark.at > 0) target.appendChild(document.createTextNode(rest.slice(0, mark.at)));
if (mark.link) {
var href = safeHref(mark.href);
if (href) {
var a = document.createElement("a");
a.href = href;
a.target = "_blank";
a.rel = "noopener noreferrer";
inline(a, mark.label);
target.appendChild(a);
} else {
target.appendChild(document.createTextNode(mark.label));
}
} else {
var el = document.createElement(mark.tag);
if (mark.cls) el.className = mark.cls;
if (mark.tag === "code") el.textContent = mark.label; else inline(el, mark.label);
target.appendChild(el);
}
rest = rest.slice(mark.at + mark.len);
}
if (rest) target.appendChild(document.createTextNode(rest));
}
function block(tag, cls, text) {
var el = document.createElement(tag);
el.className = cls;
inline(el, text);
return el;
}
// a single newline is a line break, the way it is typed
function paragraphBlock(lines) {
var el = document.createElement("p");
el.className = "annP";
for (var i = 0; i < lines.length; i++) {
if (i > 0) el.appendChild(document.createElement("br"));
inline(el, lines[i]);
}
return el;
}
function render(text, target) {
target.textContent = "";
var lines = String(text == null ? "" : text).replace(/\r\n/g, "\n").split("\n");
var paragraph = [];
var list = null;
function flushParagraph() {
if (!paragraph.length) return;
target.appendChild(paragraphBlock(paragraph));
paragraph = [];
}
function flushList() {
list = null;
}
for (var i = 0; i < lines.length; i++) {
var line = lines[i].trim();
if (!line) { flushParagraph(); flushList(); continue; }
if (/^(---|___|\*\*\*)$/.test(line)) {
flushParagraph();
flushList();
var hr = document.createElement("hr");
hr.className = "annHr";
target.appendChild(hr);
continue;
}
if (/^###\s+/.test(line)) {
flushParagraph(); flushList();
target.appendChild(block("h4", "annH3", line.replace(/^###\s+/, "")));
continue;
}
if (/^##\s+/.test(line)) {
flushParagraph(); flushList();
target.appendChild(block("h3", "annH2", line.replace(/^##\s+/, "")));
continue;
}
if (/^#\s+/.test(line)) {
flushParagraph(); flushList();
target.appendChild(block("h2", "annH1", line.replace(/^#\s+/, "")));
continue;
}
if (/^>\s?/.test(line)) {
flushParagraph(); flushList();
target.appendChild(block("blockquote", "annQuote", line.replace(/^>\s?/, "")));
continue;
}
if (/^[-*]\s+/.test(line)) {
flushParagraph();
if (!list || list.tagName !== "UL") {
list = document.createElement("ul");
list.className = "annList";
target.appendChild(list);
}
list.appendChild(block("li", "annItem", line.replace(/^[-*]\s+/, "")));
continue;
}
if (/^\d+[.)]\s+/.test(line)) {
flushParagraph();
if (!list || list.tagName !== "OL") {
list = document.createElement("ol");
list.className = "annList";
target.appendChild(list);
}
list.appendChild(block("li", "annItem", line.replace(/^\d+[.)]\s+/, "")));
continue;
}
flushList();
paragraph.push(line);
}
flushParagraph();
return target;
}
// section for the collapsed preview line
function summary(text, max) {
var limit = max || 140;
var lines = String(text == null ? "" : text).replace(/\r\n/g, "\n").split("\n");
for (var i = 0; i < lines.length; i++) {
var line = lines[i].trim();
if (!line) continue;
if (/^(---|___|\*\*\*)$/.test(line)) continue;
line = line.replace(/^#{1,3}\s+/, "").replace(/^>\s?/, "").replace(/^[-*]\s+/, "")
.replace(/^\d+[.)]\s+/, "");
var holder = document.createElement("div");
inline(holder, line);
var plain = holder.textContent.trim();
if (!plain) continue;
return plain.length > limit ? plain.slice(0, limit - 1).trimEnd() + "…" : plain;
}
return "";
}
window.zoneoutAnnounce = { render: render, summary: summary };
})();