-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.ts
104 lines (95 loc) · 2.51 KB
/
template.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
import { Liquid } from 'liquidjs'
import axios from 'axios'
import he from 'he'
import { split } from './shlex'
export const liquid = new Liquid()
liquid.registerTag('link', {
parse (token) {
this.href = token
},
async render () {
const meta = (
await axios.get('/api/metadata', {
params: {
q: this.href
}
})
).data
const img = `
<img style="${'max-width: 200px; margin-right: 1em; width: 100%; height: auto;'}" ${
meta
? meta.image
? `src="${encodeURI(meta.image)}" ` +
`alt="${he.encode(meta.title || meta.url)}" `
: ''
: ''
} />`
const innerHTML = `${
meta.image
? `
<div style="${'max-width: 200px; margin-right: 1em;' +
'display: flex; align-items: center; justify-content: center;' +
'overflow: hidden;'}">${img}
</div>`
: ''
}
<div>
${
meta.title
? `<h3 style="color: darkblue; margin-block-start: 0;">${he.encode(
meta.title
)}</h3>`
: `<h6 style="color: darkblue; margin-block-start: 0;">${he.encode(
meta.url
)}</h6>`
}
${he.encode(meta.description || '')}
</div>`
return `
<a is="a-card" style="${`flex-direction: ${'row'};` +
'display: flex;' +
'margin: 1em; padding: 1em;' +
'box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);'}" href="${encodeURI(
meta.url
)}"
rel="noopener" target="_blank">
${innerHTML}
</a>`
}
})
liquid.registerTag('youtube', {
parse (token) {
this.href = split(token.args)[0]
},
render () {
return this.href
? `
<iframe
width="560" height="315"
style="height: 315px;"
src="https://www.youtube.com/embed/${encodeURIComponent(this.href)}"
frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen
></iframe>`
: ''
}
})
liquid.registerTag('speak', {
parse (token) {
const [href, str] = split(token.args)
this.word = href
this.lang = str || 'zh'
},
render () {
const href = `https://speak-btn.now.sh/btn?q=${encodeURIComponent(
this.word
)}&lang=${encodeURIComponent(this.lang)}`
return this.word
? `
<iframe
src="${href}"
style="width: 20px; height: 20px; display: inline-block;"
frameborder="0" allowtransparency="true"
></iframe>`
: ''
}
})