FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
docsify/test/helpers/docsify-init.js at develop · docsifyjs/docsify · GitHub
docsifyjs
docsify
Repository navigation
Code
Issues
63
(63)
Pull requests
42
(42)
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
docsify
/
test
/
helpers
/
docsify-init.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
381 lines (334 loc) · 11 KB
Breadcrumbs
docsify
/
test
/
helpers
/
docsify-init.js
Copy path
File metadata and controls
381 lines (334 loc) · 11 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* globals page */
import
_mock
,
{
proxy
}
from
'xhr-mock'
;
import
axios
from
'axios'
;
import
prettier
from
'prettier'
;
import
{
stripIndent
}
from
'common-tags'
;
import
{
waitForSelector
}
from
'./wait-for.js'
;
const
mock
=
_mock
.
default
;
const
docsifyPATH
=
'../../dist/docsify.js'
;
// JSDOM
const
docsifyURL
=
'/dist/docsify.js'
;
// Playwright
/**
* Jest / Playwright helper for creating custom docsify test sites
*
*
@param
{
Object
} options options object
*
@param
{
Function|Object
} [options.config] docsify configuration (merged with default)
*
@param
{
String
} [options.html] HTML content to use for docsify `index.html` page
*
@param
{
Object
} [options.markdown] Docsify markdown content
*
@param
{
String
} [options.markdown.coverpage] coverpage markdown
*
@param
{
String
} [options.markdown.homepage] homepage markdown
*
@param
{
String
} [options.markdown.navbar] navbar markdown
*
@param
{
String
} [options.markdown.sidebar] sidebar markdown
*
@param
{
Object
} [options.routes] custom routes defined as `{ pathOrGlob: responseText }`
*
@param
{
String
} [options.script] JS to inject via <script> tag
*
@param
{
String|String[]
} [options.scriptURLs] External JS to inject via <script src="..."> tag(s)
*
@param
{
String
} [options.style] CSS to inject via <style> tag
*
@param
{
String|String[]
} [options.styleURLs=['/dist/themes/core.css']] External CSS to inject via <link rel="stylesheet"> tag(s)
*
@param
{
String
} [options.testURL] URL to set as window.location.href
*
@param
{
String
} [options.waitForSelector='#main'] Element to wait for before returning promise
*
@param
{
Boolean|Object|String
} [options._logHTML] Logs HTML to console after initialization. Accepts CSS selector.
*
@param
{
Boolean
} [options._logHTML.format=true] Formats HTML output
*
@param
{
String
} [options._logHTML.selector='html'] CSS selector(s) to match and log HTML for
*
@returns
{
Promise
}
*/
async
function
docsifyInit
(
options
=
{
}
)
{
const
isJSDOM
=
'window'
in
global
;
const
isPlaywright
=
'page'
in
global
;
const
defaults
=
{
config
:
{
basePath
:
process
.
env
.
TEST_HOST
,
el
:
'#app'
,
}
,
html
:
`
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<div id="app"></div>
</body>
</html>
`
,
markdown
:
{
coverpage
:
''
,
homepage
:
''
,
navbar
:
''
,
sidebar
:
''
,
}
,
routes
:
{
}
,
script
:
''
,
scriptURLs
:
[
]
,
style
:
''
,
styleURLs
:
[
]
,
testURL
:
`
${
process
.
env
.
TEST_HOST
}
/docsify-init.html`
,
waitForSelector
:
'#main > *:first-child'
,
}
;
const
settings
=
{
...
defaults
,
...
options
,
get
config
(
)
{
const
sharedConfig
=
{
...
defaults
.
config
,
// Conditionally set options but allow explicit overrides
coverpage
:
Boolean
(
settings
.
markdown
.
coverpage
)
,
loadNavbar
:
Boolean
(
settings
.
markdown
.
navbar
)
,
loadSidebar
:
Boolean
(
settings
.
markdown
.
sidebar
)
,
}
;
const
updateBasePath
=
config
=>
{
if
(
config
.
basePath
)
{
config
.
basePath
=
new
URL
(
config
.
basePath
,
process
.
env
.
TEST_HOST
,
)
.
href
;
}
}
;
// Config as function
if
(
typeof
options
.
config
===
'function'
)
{
return
function
(
vm
)
{
const
config
=
{
...
sharedConfig
,
...
options
.
config
(
vm
)
}
;
updateBasePath
(
config
)
;
return
config
;
}
;
}
// Config as object
else
{
const
config
=
{
...
sharedConfig
,
...
options
.
config
}
;
updateBasePath
(
config
)
;
return
config
;
}
}
,
get
markdown
(
)
{
return
Object
.
fromEntries
(
Object
.
entries
(
{
...
defaults
.
markdown
,
...
options
.
markdown
,
}
)
.
filter
(
(
[
key
,
markdown
]
)
=>
key
&&
markdown
)
.
map
(
(
[
key
,
markdown
]
)
=>
[
key
,
stripIndent
`
${
markdown
}
`
]
)
,
)
;
}
,
get
routes
(
)
{
const
helperRoutes
=
{
[
settings
.
testURL
]
:
stripIndent
`
${
settings
.
html
}
`
,
'README.md'
:
settings
.
markdown
.
homepage
,
'_coverpage.md'
:
settings
.
markdown
.
coverpage
,
'_navbar.md'
:
settings
.
markdown
.
navbar
,
'_sidebar.md'
:
settings
.
markdown
.
sidebar
,
}
;
const
finalRoutes
=
Object
.
fromEntries
(
Object
.
entries
(
{
...
helperRoutes
,
...
options
.
routes
,
}
)
// Remove items with falsey responseText
.
filter
(
(
[
url
,
responseText
]
)
=>
url
&&
responseText
)
.
map
(
(
[
url
,
responseText
]
)
=>
[
// Convert relative to absolute URL
new
URL
(
url
,
settings
.
config
.
basePath
||
process
.
env
.
TEST_HOST
)
.
href
,
// Strip indentation from responseText
stripIndent
`
${
responseText
}
`
,
]
)
,
)
;
return
finalRoutes
;
}
,
// Merge scripts and remove duplicates
scriptURLs
:
[
]
.
concat
(
options
.
scriptURLs
||
''
)
.
filter
(
url
=>
url
)
.
map
(
url
=>
new
URL
(
url
,
process
.
env
.
TEST_HOST
)
.
href
)
,
styleURLs
:
[
]
.
concat
(
options
.
styleURLs
||
''
)
.
filter
(
url
=>
url
)
.
map
(
url
=>
new
URL
(
url
,
process
.
env
.
TEST_HOST
)
.
href
)
,
}
;
// Routes
const
contentTypes
=
{
css
:
'text/css'
,
html
:
'text/html'
,
js
:
'application/javascript'
,
json
:
'application/json'
,
md
:
'text/markdown'
,
}
;
const
reFileExtentionFromURL
=
new
RegExp
(
'(?:.)('
+
Object
.
keys
(
contentTypes
)
.
join
(
'|'
)
+
')(?:[?#].*)?$'
,
'i'
,
)
;
if
(
isJSDOM
)
{
// Replace the global XMLHttpRequest object
mock
.
setup
(
)
;
}
for
(
let
[
urlGlob
,
response
]
of
Object
.
entries
(
settings
.
routes
)
)
{
const
fileExtension
=
(
urlGlob
.
match
(
reFileExtentionFromURL
)
||
[
]
)
[
1
]
;
const
contentType
=
contentTypes
[
fileExtension
]
;
if
(
typeof
response
===
'string'
)
{
response
=
{
status
:
200
,
body
:
response
,
}
;
}
// Specifying contentType required for Webkit
response
.
contentType
=
response
.
contentType
||
contentType
||
''
;
if
(
isJSDOM
)
{
mock
.
get
(
urlGlob
,
(
req
,
res
)
=>
{
return
res
.
status
(
response
.
status
)
.
body
(
settings
.
routes
[
urlGlob
]
)
.
header
(
'Content-Type'
,
contentType
)
;
}
)
;
}
else
{
await
page
.
route
(
urlGlob
,
route
=>
route
.
fulfill
(
response
)
)
;
}
}
if
(
isJSDOM
)
{
// Proxy unhandled requests to real server(s)
mock
.
use
(
proxy
)
;
}
// Set test URL / HTML
if
(
isJSDOM
)
{
window
.
history
.
pushState
(
{
}
,
'docsify'
,
settings
.
testURL
)
;
document
.
documentElement
.
innerHTML
=
settings
.
html
;
}
else
if
(
isPlaywright
)
{
await
page
.
goto
(
settings
.
testURL
)
;
await
page
.
waitForLoadState
(
)
;
}
// Docsify configuration
if
(
isJSDOM
)
{
window
.
$docsify
=
settings
.
config
;
}
else
if
(
isPlaywright
)
{
// Convert config functions to strings
const
configString
=
JSON
.
stringify
(
settings
.
config
,
(
key
,
val
)
=>
typeof
val
===
'function'
?
`__FN__
${
val
.
toString
(
)
}
`
:
val
,
)
;
await
page
.
evaluate
(
config
=>
{
// Restore config functions from strings
const
configObj
=
JSON
.
parse
(
config
,
(
key
,
val
)
=>
{
if
(
/
^
_
_
F
N
_
_
/
.
test
(
val
)
)
{
let
source
=
val
.
split
(
'__FN__'
)
[
1
]
;
// f.e. `foo() {}` or `'bar!?'() {}` without the `function ` prefix
const
isConcise
=
!
source
.
includes
(
'function'
)
&&
!
source
.
includes
(
'=>'
)
;
if
(
isConcise
)
{
source
=
`{
${
source
}
}`
;
}
else
{
source
=
`{ _:
${
source
}
}`
;
}
return
new
Function
(
/* js */
`
const o =
${
source
}
const keys = Object.keys(o)
return o[keys[0]]
`
)
(
)
;
}
else
{
return
val
;
}
}
)
;
window
.
$docsify
=
configObj
;
}
,
configString
)
;
}
// Style URLs
if
(
isJSDOM
)
{
for
(
const
url
of
settings
.
styleURLs
)
{
const
linkElm
=
document
.
createElement
(
'link'
)
;
linkElm
.
setAttribute
(
'rel'
,
'stylesheet'
)
;
linkElm
.
setAttribute
(
'type'
,
'text/css'
)
;
linkElm
.
setAttribute
(
'href'
,
url
)
;
document
.
head
.
appendChild
(
linkElm
)
;
}
}
else
if
(
isPlaywright
)
{
await
Promise
.
all
(
settings
.
styleURLs
.
map
(
url
=>
page
.
addStyleTag
(
{
url
}
)
)
)
;
}
// JavaScript URLs
if
(
isJSDOM
)
{
for
(
const
url
of
settings
.
scriptURLs
)
{
const
responseText
=
settings
.
routes
[
url
]
||
(
await
axios
.
get
(
url
)
)
.
data
;
const
scriptElm
=
document
.
createElement
(
'script'
)
;
scriptElm
.
setAttribute
(
'data-src'
,
url
)
;
scriptElm
.
textContent
=
stripIndent
`
${
responseText
}
`
;
document
.
head
.
appendChild
(
scriptElm
)
;
}
const
isDocsifyLoaded
=
'Docsify'
in
window
;
if
(
!
isDocsifyLoaded
)
{
await
import
(
docsifyPATH
)
;
}
}
else
if
(
isPlaywright
)
{
for
(
const
url
of
settings
.
scriptURLs
)
{
await
page
.
addScriptTag
(
{
url
}
)
;
}
const
isDocsifyLoaded
=
await
page
.
evaluate
(
(
)
=>
'Docsify'
in
window
)
;
if
(
!
isDocsifyLoaded
)
{
await
page
.
addScriptTag
(
{
url
:
docsifyURL
}
)
;
}
}
// Style
if
(
settings
.
style
)
{
if
(
isJSDOM
)
{
const
headElm
=
document
.
querySelector
(
'head'
)
;
const
styleElm
=
document
.
createElement
(
'style'
)
;
styleElm
.
textContent
=
stripIndent
`
${
settings
.
style
}
`
;
headElm
.
appendChild
(
styleElm
)
;
}
else
if
(
isPlaywright
)
{
await
page
.
evaluate
(
data
=>
{
const
headElm
=
document
.
querySelector
(
'head'
)
;
const
styleElm
=
document
.
createElement
(
'style'
)
;
styleElm
.
textContent
=
data
;
headElm
.
appendChild
(
styleElm
)
;
}
,
stripIndent
`
${
settings
.
style
}
`
,
)
;
}
}
// JavaScript
if
(
settings
.
script
)
{
if
(
isJSDOM
)
{
const
scriptElm
=
document
.
createElement
(
'script'
)
;
scriptElm
.
textContent
=
stripIndent
`
${
settings
.
script
}
`
;
document
.
head
.
appendChild
(
scriptElm
)
;
}
else
if
(
isPlaywright
)
{
await
page
.
addScriptTag
(
{
content
:
settings
.
script
}
)
;
}
}
// Docsify "Ready"
if
(
isJSDOM
)
{
await
waitForSelector
(
settings
.
waitForSelector
)
;
}
else
if
(
isPlaywright
)
{
// await page.waitForSelector(settings.waitForSelector);
await
page
.
locator
(
settings
.
waitForSelector
)
.
waitFor
(
)
;
await
page
.
waitForLoadState
(
'load'
)
;
}
// Log HTML to console
if
(
settings
.
_logHTML
)
{
const
selector
=
typeof
settings
.
_logHTML
===
'string'
?
settings
.
_logHTML
:
settings
.
_logHTML
.
selector
;
let
htmlArr
=
[
]
;
if
(
selector
)
{
if
(
isJSDOM
)
{
htmlArr
=
[
...
document
.
querySelectorAll
(
selector
)
]
.
map
(
elm
=>
elm
.
outerHTML
,
)
;
}
else
{
htmlArr
=
await
page
.
evaluateAll
(
selector
,
elms
=>
elms
.
map
(
e
=>
e
.
outerHTML
)
,
)
;
}
}
else
{
htmlArr
=
[
isJSDOM
?
document
.
documentElement
.
outerHTML
:
await
page
.
content
(
)
,
]
;
}
if
(
htmlArr
.
length
)
{
htmlArr
.
forEach
(
html
=>
{
if
(
settings
.
_logHTML
.
format
!==
false
)
{
html
=
prettier
.
format
(
html
,
{
parser
:
'html'
}
)
;
}
console
.
log
(
html
)
;
}
)
;
}
else
{
console
.
warn
(
`docsify-init(): unable to match selector '
${
selector
}
'`
)
;
}
}
return
Promise
.
resolve
(
)
;
}
export
default
docsifyInit
;
Back
|
FazBrowse Home
|
New Git URL