Optimize loading speed (#45)

This commit is contained in:
AceDataCloud committed 2024-05-26 04:02:03 +00:00
1 parent a18db299ee
commit a0bcce5cf1
9 files changed
+92 -33

No files matched your search

+25
View File
@@ -0,0 +1,25 @@
import i18n from '@/i18n';
import copyToClipboard from 'copy-to-clipboard';
export const highlight = async (el: HTMLElement) => {
const hl = (await import('highlight.js')).default;
console.debug('highlight.js loaded');
const blocks = el.querySelectorAll('pre code');
blocks.forEach((block) => {
// create the copy button
const copy = document.createElement('button');
copy.innerHTML = i18n.global.t('common.button.copy').toString();
// add the event listener to each click
copy.addEventListener('click', () => {
// @ts-ignore
copyToClipboard(block.innerText);
});
// append the copy button to each code block
// check already has copy button
if (!block.parentElement?.querySelector('button')) {
block.parentElement?.prepend(copy);
}
// @ts-ignore
hl.highlightBlock(block);
});
};
+3
View File
@@ -1,3 +1,6 @@
export * from './baseUrl';
export * from './mode';
export * from './log';
export * from './initializer';
export * from './highlight';
export * from './is';