-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathemoji.ts
141 lines (119 loc) · 4.06 KB
/
emoji.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import marpitPlugin from '@marp-team/marpit/plugin'
import twemoji from '@twemoji/api'
import emojiRegex from 'emoji-regex'
import { full as markdownItEmoji } from 'markdown-it-emoji'
import twemojiCSS from './twemoji.scss'
export interface EmojiOptions {
shortcode?: boolean | 'twemoji'
twemoji?: TwemojiOptions
unicode?: boolean | 'twemoji'
}
interface TwemojiOptions {
base?: string
ext?: 'svg' | 'png'
}
const regexForSplit = new RegExp(`(${emojiRegex().source})(?!\uFE0E)`, 'g')
export const css = (opts: EmojiOptions) =>
opts.shortcode === 'twemoji' || opts.unicode === 'twemoji'
? twemojiCSS
: undefined
export const markdown = marpitPlugin((md) => {
const opts: EmojiOptions = md.marpit.options.emoji
const twemojiOpts = opts.twemoji || {}
const twemojiExt = twemojiOpts.ext || 'svg'
const twemojiParse = (content: string): string =>
twemoji.parse(content, {
attributes: () => ({ 'data-marp-twemoji': '' }),
base: twemojiOpts.base || undefined,
ext: `.${twemojiExt}`,
size: twemojiExt === 'svg' ? 'svg' : undefined,
})
const twemojiRenderer = (token: any[], idx: number): string =>
twemojiParse(token[idx].content)
if (opts.shortcode) {
// Pick rules to avoid collision with other markdown-it plugin
const picker = {
core: {
ruler: {
push: (_, rule) => (picker.rule = rule), // for markdown-it-emoji <= v2.0.0
after: (_, __, rule) => (picker.rule = rule), // for markdown-it-emoji >= v2.0.1
},
},
renderer: { rules: { emoji: () => {} } },
rule: (() => {}) as (...args: any[]) => void,
utils: md.utils,
}
markdownItEmoji(picker, { shortcuts: {} })
// TODO: use md.core.ruler.after
md.core.ruler.push('marp_emoji', (state) => {
const { Token } = state
state.Token = function replacedToken(name, ...args) {
return new Token(name === 'emoji' ? 'marp_emoji' : name, ...args)
}
picker.rule(state)
state.Token = Token
})
md.renderer.rules.marp_emoji =
opts.shortcode === 'twemoji'
? twemojiRenderer
: picker.renderer.rules.emoji
}
if (opts.unicode) {
md.core.ruler.after('inline', 'marp_unicode_emoji', ({ tokens, Token }) => {
for (const token of tokens) {
if (token.type === 'inline') {
const newChildren: any[] = []
for (const t of token.children) {
if (t.type === 'text') {
const splittedByEmoji = t.content.split(regexForSplit)
newChildren.push(
...splittedByEmoji.reduce(
(splitedArr, text, idx) =>
text.length === 0
? splitedArr
: [
...splitedArr,
Object.assign(new Token(), {
...t,
content: text,
type: idx % 2 ? 'marp_unicode_emoji' : 'text',
}),
],
[],
),
)
} else {
newChildren.push(t)
}
}
token.children = newChildren
}
}
})
md.renderer.rules.marp_unicode_emoji = (
token: any[],
idx: number,
): string => token[idx].content
const { code_block, code_inline, fence } = md.renderer.rules
if (opts.unicode === 'twemoji') {
const wrap = (text) =>
text
.split(/(<[^>]*>)/g)
.reduce(
(ret, part, idx) =>
`${ret}${
idx % 2
? part
: part.replace(regexForSplit, ([emoji]) =>
twemojiParse(emoji),
)
}`,
'',
)
md.renderer.rules.marp_unicode_emoji = twemojiRenderer
md.renderer.rules.code_inline = (...args) => wrap(code_inline(...args))
md.renderer.rules.code_block = (...args) => wrap(code_block(...args))
md.renderer.rules.fence = (...args) => wrap(fence(...args))
}
}
})