Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

KaTeX support #174

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"insert-text-at-cursor": "0.3.0",
"is-file-animated": "1.0.2",
"json5": "2.2.3",
"katex": "^0.16.9",
"matter-js": "0.19.0",
"mfm-js": "0.23.3",
"misskey-js": "workspace:*",
Expand Down
25 changes: 25 additions & 0 deletions packages/frontend/src/components/MkFormula.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<template>
<XFormula :formula="formula" :block="block" />
</template>

<script lang="ts">
import { defineAsyncComponent, defineComponent } from "vue";

export default defineComponent({
components: {
XFormula: defineAsyncComponent(
() => import("@/components/MkFormulaCore.vue"),
),
},
props: {
formula: {
type: String,
required: true,
},
block: {
type: Boolean,
required: true,
},
},
});
</script>
36 changes: 36 additions & 0 deletions packages/frontend/src/components/MkFormulaCore.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<template>
<div v-if="block" v-html="compiledFormula"></div>
<span v-else v-html="compiledFormula"></span>
</template>

<script lang="ts">
import { defineComponent } from "vue";
import katex from "katex";

export default defineComponent({
props: {
formula: {
type: String,
required: true,
},
block: {
type: Boolean,
required: true,
},
},
computed: {
compiledFormula(): any {
const katexString = katex.renderToString(this.formula, {
throwOnError: false,
} as any);
return this.block
? `<div style="text-align:center">${katexString}</div>`
Copy link
Contributor

@wont-work wont-work Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel like this would be better as part of the vue template instead of being inline html, with text-align:center applied via <style> instead of being inline

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some kind of simple HTML sanitization could be made here, depending on what KaTeX renders to you might be able to just drop all non-math HTML tags and their attributes

: katexString;
},
},
});
</script>

<style>
@import "../../node_modules/katex/dist/katex.min.css";
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import MkLink from '@/components/MkLink.vue';
import MkMention from '@/components/MkMention.vue';
import MkEmoji from '@/components/global/MkEmoji.vue';
import MkCustomEmoji from '@/components/global/MkCustomEmoji.vue';
import MkFormula from "@/components/MkFormula.vue";
import MkCode from '@/components/MkCode.vue';
import MkGoogle from '@/components/MkGoogle.vue';
import MkSparkle from '@/components/MkSparkle.vue';
Expand Down Expand Up @@ -389,12 +390,24 @@ export default function(props: MfmProps) {
})];
}

case 'mathInline': {
return [h('code', token.props.formula)];
case "mathInline": {
return [
h(MkFormula, {
key: Math.random(),
formula: token.props.formula,
block: false,
}),
];
}

case 'mathBlock': {
return [h('code', token.props.formula)];
case "mathBlock": {
return [
h(MkFormula, {
key: Math.random(),
formula: token.props.formula,
block: true,
}),
];
}

case 'search': {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.