Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加代码插件 #704

Merged
merged 9 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/client/utils/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@ const PRISM_CDN = 'https://cdn.staticfile.net/prism/1.28.0'
let Prism
let cssEl

const renderCode = (el, theme) => {
const renderCode = (el, theme, plugins) => {
const prismCdn = (app && app.$twikoo.prismCdn) ? app.$twikoo.prismCdn : PRISM_CDN
window.Prism = window.Prism || {}
window.Prism.manual = true
if (!Prism) {
Prism = require('prismjs')
require('prismjs/plugins/autoloader/prism-autoloader')
Prism.plugins.autoloader.languages_path = `${prismCdn}/components/`
// require('prismjs/plugins/toolbar/prism-toolbar')
// require('prismjs/plugins/show-language/prism-show-language')
if (plugins) {
require('prismjs/plugins/toolbar/prism-toolbar')
plugins.split(',').map(item => { return item.trim() }).forEach(p => {
if (p === 'showLanguage') {
require('prismjs/plugins/show-language/prism-show-language')
} else if (p === 'copyButton') {
require('prismjs/plugins/copy-to-clipboard/prism-copy-to-clipboard')
}
})
}
}
loadCss(theme, prismCdn)
Prism.highlightAllUnder(el)
Expand Down
13 changes: 13 additions & 0 deletions src/client/utils/i18n/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ const highlightThemes = [
'twilight'
].map(s => `"${s}"`)

const highlightPlugins = [
'showLanguage',
'copyButton'
].map(s => `"${s}"`)

const imageBedServices = [
'qcloud',
'7bu',
Expand Down Expand Up @@ -455,6 +460,14 @@ export default {
`Кодни таъкидлаш мавзуси. Қуйидагилардан танланг: ${highlightThemes.join(', ')}. Олдиндан кўриш учун https://prismjs.com сайтига ташриф буюринг. Агар мавзуингиз кодни ажратиб кўрсатишга зид бўлса, уни «Ҳеч» га ўрнатинг. Стандарт: йўқ.`,
`コードハイライトのテーマ、選択肢:${highlightThemes.join('、')}、テーマの効果をプレビューするには https://prismjs.com を訪問してください。テーマとコードハイライトに競合がある場合、noneに設定してください。デフォルト:none`
],
[S.ACI + '_HIGHLIGHT_PLUGIN']: [
`代码高亮插件,可选:${highlightPlugins.join('、')},分别表示:展示代码语言、展示代码拷贝按钮。可以同时设置多个选项,如果想要不添加任何代码高亮插件,请设为 none。默认:none。`,
`代碼高亮插件,可選:${highlightPlugins.join('、')},分別表示:展示代碼語言、展示代碼拷貝按鈕。可以同時設置多個選項,如果想要不添加任何代碼高亮插件,請設為 none。預設:none。`,
`代碼高亮外掛程式,可選:${highlightPlugins.join('、')},分別表示:展示代碼語言、展示代碼拷貝按鈕。 可以同時設置多個選項,如果想要不添加任何代碼高亮外掛程式,請設定為 none。預設:none。`,
`Code highlight plug-in, optional: ${highlightPlugins.join(', ')}, respectively: show code language, show code copy button. Multiple options can be set at the same time, if you want to add no code highlighting plug-ins, please set it to none. Default: none. `,
`Плагин подсветки кода, опционально: ${highlightPlugins.join(',')}, соответственно: показывать язык кода, показывать кнопку копирования кода. Вы можете установить несколько опций одновременно, если вы хотите не добавлять плагин подсветки кода, установите значение none. по умолчанию: none.`,
`コード・ハイライト・プラグイン、オプション: ${highlightPlugins.join(',')}, それぞれ: コード言語の表示、コード・コピー・ボタンの表示。 複数のオプションを同時に設定できますが、コード・ハイライト・プラグインを追加したくない場合は、noneに設定してください。`
],
[S.ACI + '_IMAGE_CDN']: [
`插入图片所使用的图床,目前支持:${imageBedServices.join('、')}`,
`插入圖片所使用的圖床,目前支持:${imageBedServices.join('、')}`,
Expand Down
2 changes: 1 addition & 1 deletion src/client/view/components/TkAdminComment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export default {
},
highlightCode () {
if (this.serverConfig.HIGHLIGHT === 'true') {
renderCode(this.$refs['comment-list'], this.serverConfig.HIGHLIGHT_THEME)
renderCode(this.$refs['comment-list'], this.serverConfig.HIGHLIGHT_THEME, this.serverConfig.HIGHLIGHT_PLUGIN)
}
}
},
Expand Down
1 change: 1 addition & 0 deletions src/client/view/components/TkAdminConfig.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export default {
{ key: 'EMOTION_CDN', desc: t('ADMIN_CONFIG_ITEM_EMOTION_CDN'), ph: '', value: '' },
{ key: 'HIGHLIGHT', desc: t('ADMIN_CONFIG_ITEM_HIGHLIGHT'), ph: `${t('ADMIN_CONFIG_EXAMPLE')}false`, value: '' },
{ key: 'HIGHLIGHT_THEME', desc: t('ADMIN_CONFIG_ITEM_HIGHLIGHT_THEME'), ph: `${t('ADMIN_CONFIG_EXAMPLE')}tomorrow`, value: '' },
{ key: 'HIGHLIGHT_PLUGIN', desc: t('ADMIN_CONFIG_ITEM_HIGHLIGHT_PLUGIN'), ph: `${t('ADMIN_CONFIG_EXAMPLE')}showLanguage`, value: '' },
{ key: 'LIGHTBOX', desc: t('ADMIN_CONFIG_ITEM_LIGHTBOX'), ph: `${t('ADMIN_CONFIG_EXAMPLE')}true`, value: '' }
]
},
Expand Down
4 changes: 2 additions & 2 deletions src/client/view/components/TkComment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export default {
}
},
showContentExpandIfNeed () {
this.hasContentExpand = this.$refs['tk-content'].scrollHeight > 500
this.hasContentExpand = this.hasContentExpand || this.$refs['tk-content'].scrollHeight > 500 // 如果已经折叠就不再判断 主要是为了防止图片在onload之前就已经折叠而导致图片在onload之后取消折叠
},
showContentExpandIfNeedAfterImagesLoaded () {
this.$refs['tk-content'].querySelectorAll('img').forEach((imgEl) => {
Expand Down Expand Up @@ -334,7 +334,7 @@ export default {
handler: function (highlight) {
if (highlight === 'true') {
this.$nextTick(() => {
renderCode(this.$refs.comment, this.config.HIGHLIGHT_THEME)
renderCode(this.$refs.comment, this.config.HIGHLIGHT_THEME, this.config.HIGHLIGHT_PLUGIN)
})
}
},
Expand Down
27 changes: 27 additions & 0 deletions src/client/view/components/TkComments.vue
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,31 @@ export default {
cursor: pointer;
color: #409eff;
}
div.code-toolbar {
HcGys marked this conversation as resolved.
Show resolved Hide resolved
position: relative;
border-radius: .3em
}
div.code-toolbar>.toolbar {
HcGys marked this conversation as resolved.
Show resolved Hide resolved
position: absolute;
right: 4px;
top: 4px;
font-size: .8125rem;
font-weight: 500;
display: flex;
}
div.code-toolbar>.toolbar>.toolbar-item {
HcGys marked this conversation as resolved.
Show resolved Hide resolved
margin-left: .3em
}
div.code-toolbar>.toolbar>.toolbar-item>a,
div.code-toolbar>.toolbar>.toolbar-item>button,
div.code-toolbar>.toolbar>.toolbar-item>span {
HcGys marked this conversation as resolved.
Show resolved Hide resolved
padding: 2px 4px;
border-radius: .3em;
}
div.code-toolbar>.toolbar>.toolbar-item>button {
HcGys marked this conversation as resolved.
Show resolved Hide resolved
border: 1px solid rgba(128, 128, 128, 0.31);
}
div.code-toolbar>.toolbar>.toolbar-item>button:hover {
HcGys marked this conversation as resolved.
Show resolved Hide resolved
cursor: pointer;
}
</style>
2 changes: 1 addition & 1 deletion src/client/view/components/TkSubmit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export default {
renderLinks(this.$refs['comment-preview'])
renderMath(this.$refs['comment-preview'], this.$twikoo.katex)
if (this.config.HIGHLIGHT === 'true') {
renderCode(this.$refs['comment-preview'], this.config.HIGHLIGHT_THEME)
renderCode(this.$refs['comment-preview'], this.config.HIGHLIGHT_THEME, this.config.HIGHLIGHT_PLUGIN)
}
})
}
Expand Down
1 change: 1 addition & 0 deletions src/server/function/twikoo/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ const fn = {
HIDE_ADMIN_CRYPT: config.HIDE_ADMIN_CRYPT,
HIGHLIGHT: config.HIGHLIGHT || 'true',
HIGHLIGHT_THEME: config.HIGHLIGHT_THEME,
HIGHLIGHT_PLUGIN: config.HIGHLIGHT_PLUGIN,
LIMIT_LENGTH: config.LIMIT_LENGTH,
TURNSTILE_SITE_KEY: config.TURNSTILE_SITE_KEY
}
Expand Down
Loading