-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
120 lines (106 loc) · 2.94 KB
/
index.ts
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
/*
* @dimerapp/docs-theme
*
* (c) DimerApp
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import type { Edge } from 'edge.js'
import type { PipelineHook } from '@dimerapp/edge/types'
/**
* The pipeline hook to use custom templates for certain
* markdown nodes
*/
export const docsHook: PipelineHook = (node, pipeline) => {
if (node.tagName === 'disclosure') {
return pipeline.component('docs::elements/disclosure', { node })
}
/**
* Wrapping headings text inside a span
*/
if (node.tagName === 'h2' || node.tagName === 'h3' || node.tagName === 'h4') {
node.children = [
node.children.shift()!,
{
type: 'element',
tagName: 'span',
properties: {},
children: node.children,
},
]
}
/**
* Opening third-party URLs inside a new tab
* Opening relative in-app URLs using unpoly.
*/
if (node.tagName === 'a') {
node.properties = node.properties || {}
const url = node.properties.href
if (typeof url !== 'string') {
return
}
if (url.startsWith('https://') || url.startsWith('http://')) {
node.properties.target = '_blank'
node.properties.rel = 'noopener noreferrer'
} else if (!url.startsWith('#')) {
node.properties['up-target'] = '[layout-shell]'
node.properties['up-preload'] = ''
}
}
/**
* Rendering tables using a custom component
*/
if (node.tagName === 'table') {
return pipeline.component('docs::elements/table', { node })
}
/**
* Render pre element using a custom edge component
*/
if (node.tagName === 'pre') {
if (node.properties) {
node.properties.style = ''
}
return pipeline.component('docs::elements/pre', { node })
}
/**
* Processing include tag
*/
if (node.tagName === 'include') {
return pipeline.component('docs::elements/includes_partial', { node })
}
/**
* Processing component tag. Assuming the template property
* will be provided all the times
*/
if (node.tagName === 'component') {
return pipeline.component(node.properties!.template as string, { node })
}
if (!node.properties || !Array.isArray(node.properties.className)) {
return
}
/**
* Render codegroups using a custom component
*/
if (node.properties.className.includes('codegroup')) {
return pipeline.component('docs::elements/codegroup', { node })
}
/**
* Render alerts using a custom component
*/
if (node.properties.className.includes('alert')) {
return pipeline.component('docs::elements/alert', { node })
}
/**
* Render captions using a custom component
*/
if (node.properties.className.includes('caption')) {
return pipeline.component('docs::elements/caption', { node })
}
}
/**
* Edge plugin to mount the docs theme templates
*/
export function docsTheme(edge: Edge) {
edge.mount('docs', new URL('./templates', import.meta.url))
}