This repository has been archived by the owner on Dec 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
85 lines (70 loc) · 2.36 KB
/
index.js
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
const { Plugin } = require('powercord/entities')
const { React, getModule } = require('powercord/webpack')
const { inject, uninject } = require('powercord/injector')
const { getReactInstance } = require('powercord/util')
const Settings = require('./components/Settings.jsx')
const ShikiHighlighter = require('./components/ShikiHighlighter.jsx')
const shiki = require('./modules/shiki')
module.exports = class ShikiCodeblocks extends Plugin {
async startPlugin () {
this.loadStylesheet('style.css')
powercord.api.settings.registerSettings('vpc-shiki', {
category: this.entityID,
label: 'Shiki Codeblocks',
render: ({ getSetting, updateSetting, toggleSetting }) => React.createElement(Settings, {
getSetting,
updateSetting,
toggleSetting,
shiki,
setTheme: this.setTheme.bind(this),
refreshCodeblocks: () => this.forceUpdate()
})
})
this.patchCodeblocks()
this.shiki = shiki
shiki.init().then(() => this.setTheme())
}
pluginWillUnload () {
uninject('vpc-shiki-format')
powercord.api.settings.unregisterSettings('vpc-shiki')
this.forceUpdate()
}
async setTheme (themeId) {
if (!themeId) themeId = this.settings.get('theme', Object.keys(shiki.themes)[0])
const customThemeUrl = this.settings.get('custom-theme')
if (customThemeUrl) {
try {
await shiki.loadTheme(customThemeUrl)
themeId = customThemeUrl
} catch (error) {
console.error(error)
}
}
return await shiki.setTheme(themeId)
}
async patchCodeblocks () {
const parser = await getModule([ 'parse', 'parseTopic' ])
inject('vpc-shiki-format', parser.defaultRules.codeBlock, 'react', (args, res) => {
this.injectCodeblock(args, res)
return res
})
this.forceUpdate()
}
injectCodeblock (args, res) {
if (!args) return
res.props.render = () => {
const { lang, content } = args[0]
return React.createElement(ShikiHighlighter, {
lang,
content,
shiki,
tryHLJS: this.settings.get('try-hljs', 'never'),
useDevIcon: this.settings.get('use-devicon', 'false'),
bgOpacity: this.settings.get('bg-opacity', 100),
})
}
}
forceUpdate () {
document.querySelectorAll('[id^="chat-messages-"]').forEach(e => getReactInstance(e)?.memoizedProps?.onMouseMove?.())
}
}