This repository has been archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
92 additions
and
4 deletions.
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,79 @@ | ||
<script lang="tsx"> | ||
import 'highlight.js/styles/atom-one-light.css'; | ||
import { ref, computed, defineComponent, watch } from 'vue'; | ||
import 'highlight.js/lib/common'; | ||
import hljs from 'highlight.js/lib/core'; | ||
import { escapeHtml } from './utils'; | ||
export default defineComponent({ | ||
props: { | ||
code: { | ||
type: String, | ||
required: true | ||
}, | ||
lang: { | ||
type: String, | ||
default: '' | ||
}, | ||
autodetect: { | ||
type: Boolean, | ||
default: true | ||
}, | ||
ignoreIllegals: { | ||
type: Boolean, | ||
default: true | ||
} | ||
}, | ||
setup(props) { | ||
const language = ref(props.lang); | ||
watch( | ||
() => props.lang, | ||
(nv) => { | ||
language.value = nv; | ||
} | ||
); | ||
const autodetect = computed(() => props.autodetect && !language.value); | ||
const cannotDetectLanguage = computed( | ||
() => !autodetect.value && !hljs.getLanguage(language.value) | ||
); | ||
const className = computed((): string => { | ||
if (cannotDetectLanguage.value) { | ||
return ''; | ||
} | ||
return `hljs ${language.value}`; | ||
}); | ||
const highlightedCode = computed((): string => { | ||
// No idea what language to use, return raw code | ||
if (cannotDetectLanguage.value) { | ||
console.warn( | ||
`The language "${language.value}" you specified could not be found.` | ||
); | ||
return escapeHtml(props.code); | ||
} | ||
if (autodetect.value) { | ||
const result = hljs.highlightAuto(props.code); | ||
return result.value; | ||
} | ||
const result = hljs.highlight(props.code, { | ||
language: language.value, | ||
ignoreIllegals: props.ignoreIllegals | ||
}); | ||
return result.value; | ||
}); | ||
return () => ( | ||
<pre> | ||
<code | ||
class={className.value} | ||
tabindex="0" | ||
innerHTML={highlightedCode.value} | ||
></code> | ||
</pre> | ||
); | ||
} | ||
}); | ||
</script> |
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
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,10 @@ | ||
export function escapeHtml(value: string): string { | ||
return value | ||
.replace(/&/g, '&') | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>') | ||
.replace(/"/g, '"') | ||
.replace(/'/g, '''); | ||
} | ||
|
||
export default {}; |
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