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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/highlight.min.js" async></script>
<script>
const VROOT = "/";
function repoPathFromLink( a ) {
const href = new URL( a.getAttribute( "href" ), location.href );
let p = href.pathname;
if( !p.startsWith( VROOT ) ) return null;
p = p.slice( VROOT.length );
p = p.replace( /\/+$/, "" );
if( !p ) return null;
return p;
}
function branchFromForm() {
const formWrap = document.querySelector( ".form" );
const form = formWrap.querySelector( "form" );
const branchEl = form.querySelector( "select" );
if( !branchEl )
return "";
const branch = branchEl.value;
if( !branch )
return "";
return branch;
}
async function getFile( repo, branch, file ) {
try {
const res = await fetch( `https://${location.host}/${repo}/plain/${file}` );
if( !res.ok )
return null;
return await res.text();
} catch( e ) {
return null;
}
}
function isWhiteChar( char ) {
return char === " " || char === "\t" || char === "\n" || char === "\r";
}
function removeTrailingWhiteSpace( text ) {
while( text.length > 1
&& isWhiteChar( text[text.length - 1] )
&& isWhiteChar( text[text.length - 2] )
) {
text = text.slice( 0, -1 );
}
return text;
}
async function getReadmeFile( repo, branch ) {
let file = await getFile( repo, branch, "README.md" );
if( file )
return { file: removeTrailingWhiteSpace( file ), isMd: true };
file = await getFile( repo, branch, "README" );
if( file )
return { file: removeTrailingWhiteSpace( file ), isMd: false };
return null;
}
function isIndex( repo ) {
let path = location.pathname;
return path.replaceAll( "/", "" ) === repo;
}
function parseLink( text, start ) {
let isImg = 0;
const open = text.indexOf( "[", start );
if( open === -1 ) return null;
const close = text.indexOf( "]", open );
if( close === -1 ) return null;
const lopen = close + 1;
if( text[lopen] != '(' ) return { continue: true, end: lopen };
const lclose = text.indexOf( ")", lopen );
if( lclose === -1 ) return { continue: true, end: lopen };
const title = text.substring( open + 1, close );
const link = text.substring( lopen + 1, lclose );
if( open > 0 && text[open-1] == "!" ) isImg = 1;
return { title, link, start: open - (isImg?1:0), end: lclose + 1, img: isImg };
}
function parseLinks( text ) {
let start = 0;
const links = [];
while( true ) {
const res = parseLink( text, start );
if( !res )
break;
if( res.continue ) {
start = res.end + 1;
continue;
}
links.push( res );
start = res.end + 1;
}
return links;
}
function replaceLinks( text, links ) {
let offset = 0;
for( let link of links ) {
const start = link.start + offset;
const end = link.end + offset;
const textFirst = text.slice( 0, start );
const textSecond = text.slice( end );
let textMid = "";
if( link.img ) {
textMid = `<img src="${link.link}" alt="${link.title}" />`;
} else {
textMid = `<a href="${link.link}">${link.title}</a>`;
}
text = textFirst + textMid + textSecond;
offset += textMid.length - ( link.end - link.start );
}
return text;
}
function escapeHtml( html ) {
const entityMap = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'/': '/',
'`': '`',
'=': '='
};
return String( html ).replace( /[&<>"'`=\/]/g, ( s, i, str ) => {
if( str.slice( i, i + 6 ).toLowerCase() === "<code>" ) return "<";
if( s === '<' ) {
if( str.slice( i, i + 6 ).toLowerCase() === "<code>"
|| str.slice( i, i + 7 ).toLowerCase() === "</code>" ) {
return "<";
}
}
if( s === '>' ) {
if( str.slice( i - 5, i + 1 ).toLowerCase() === "<code>"
|| str.slice( i - 6, i + 1 ).toLowerCase() === "</code>" ) {
return ">";
}
}
if( s === '/' ) {
if( str.slice( i - 1, i + 6 ).toLowerCase() === "</code>" ) {
return "/";
}
}
return entityMap[s];
});
}
function replaceCodeTicks( text ) {
let inBlock = 0;
let tick = text.indexOf( "```" );
while( tick != -1 ) {
if( !inBlock ) {
let second = text.slice( tick + 3 );
while( isWhiteChar( second[0] ) )
second = second.slice( 1 );
text = text.slice( 0, tick ) + "<code>" + second;
tick = text.indexOf( "```", tick );
} else {
let second = text.slice( tick + 3 );
while( second.length > 1 && isWhiteChar( second[0] ) && isWhiteChar( second[1] ) )
second = second.slice( 1 );
text = text.slice( 0, tick ) + "</code>" + second;
tick = text.indexOf( "```", tick );
}
inBlock = !inBlock;
}
return escapeHtml( text );
}
function runHighlight() {
if( hljs === undefined || !hljs )
return setTimeout( runHighlight, 200 );
hljs.highlightAll();
}
function createReadme( text, isMd ) {
const tr = document.createElement( "tr" );
const td = document.createElement( "td" );
const pre = document.createElement( "pre" );
tr.appendChild( td );
td.appendChild( pre );
td.setAttribute( "colspan", "4" );
if( !isMd )
pre.innerHTML = text;
else {
const noTicks = replaceCodeTicks( text );
const links = parseLinks( noTicks );
pre.innerHTML = replaceLinks( noTicks, links );
}
pre.style.whiteSpace = "pre-wrap";
pre.style.fontFamily = "JPN12";
pre.style.padding = 0;
pre.style.marginTop = "0px";
pre.style.marginBottom = "0px";
tr.classList.add( "nohover" );
tr.style.backgroundColor = "#808080"
const list = document.querySelector( ".list" );
const tbody = list.querySelector( "tbody" );
let insertTarget = tbody.children[3];
for( let i = 2; i < tbody.children.length; ++i ) {
if( tbody.children[i-1].classList.contains( "nohover" ) ) {
insertTarget = tbody.children[i];
break;
}
}
tbody.insertBefore( tr, insertTarget );
const th = document.createElement( "th" );
th.classList.add( "left" );
th.setAttribute( "colspan", "4" );
th.innerHTML = "Readme";
tbody.insertBefore( th, tr );
//setTimeout( () => { setTimeout( runHighlight ) } );
}
function isCodeView( repo ) {
return location.pathname.includes( `/${repo}/tree` );
}
const main = async () => {
const mainEl = document.querySelector( ".main" );
if( !mainEl )
return;
const repoEl = mainEl.querySelectorAll( "a" )[1];
if( !repoEl )
return;
const repo = repoPathFromLink( repoEl );
if( !repo )
return;
if( isCodeView( repo ) )
return setTimeout( runHighlight );
if( !isIndex( repo ) )
return;
const branch = branchFromForm();
const { file, isMd } = await getReadmeFile( repo, branch );
createReadme( file, isMd );
}
setTimeout( main );
</script>
<link rel="stylesheet" href="https://networkheaven.net/static/highlight.css" type="text/css" />
|