forked from NateWr/ui-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkp/pkp-lib#9840 Add CodeHighlighter as component
- Loading branch information
Showing
2 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<template> | ||
<highlightjs | ||
:language="language" | ||
:code="code" | ||
/> | ||
</template> | ||
|
||
<script> | ||
import hljs from 'highlight.js/lib/core'; | ||
import hljsVuePlugin from '@highlightjs/vue-plugin'; | ||
// Importing the language modules | ||
import javascript from 'highlight.js/lib/languages/javascript'; | ||
import xml from 'highlight.js/lib/languages/xml'; | ||
import markdown from 'highlight.js/lib/languages/markdown'; | ||
import css from 'highlight.js/lib/languages/css'; | ||
import latex from 'highlight.js/lib/languages/latex'; | ||
// Add more languages | ||
// A map to store language modules | ||
const languageMap = { | ||
javascript, | ||
xml, | ||
markdown, | ||
css, | ||
latex, | ||
// Add more languages here | ||
}; | ||
const supportedLanguages = Object.keys(languageMap); | ||
export default { | ||
components: { | ||
highlightjs: hljsVuePlugin.component | ||
}, | ||
props: { | ||
code: { | ||
type: String, | ||
required: true | ||
}, | ||
language: { | ||
type: String, | ||
required: true, | ||
validator(value) { | ||
if (!supportedLanguages.includes(value)) { | ||
console.error(`Unsupported language: ${value}. Supported languages are: ${supportedLanguages.join(', ')}`); | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
}, | ||
created() { | ||
this.loadLanguageModule(this.language); | ||
}, | ||
methods: { | ||
loadLanguageModule(language) { | ||
const languageModule = languageMap[language]; | ||
if (languageModule) { | ||
hljs.registerLanguage(language, languageModule); | ||
} else { | ||
console.error(`Language module for ${language} is not available.`); | ||
} | ||
} | ||
} | ||
}; | ||
</script> | ||
|
||
<style> | ||
@import 'highlight.js/styles/default.css'; | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters