Skip to content
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
5 changes: 3 additions & 2 deletions packages/article-converter/src/plugins/mathPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
*/

import { attributesToProps } from "html-react-parser";
import { MathEmbed } from "@ndla/ui";
import { type PluginType } from "./types";

export const mathPlugin: PluginType = (node) => {
const { "data-math": mathContent, ...props } = attributesToProps(node.attribs);
// @ts-expect-error - math is a valid tag
return <math {...props} dangerouslySetInnerHTML={{ __html: mathContent }} />;
return <MathEmbed mathContent={mathContent as string} {...props} />;
};
29 changes: 29 additions & 0 deletions packages/ndla-ui/src/Embed/MathEmbed.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (c) 2025-present, NDLA.
*
* This source code is licensed under the GPLv3 license found in the
* LICENSE file in the root directory of this source tree.
*
*/

import { useEffect, useRef, type ComponentProps } from "react";

interface Props extends ComponentProps<"span"> {
mathContent: string;
}

export const MathEmbed = ({ mathContent, ...props }: Props) => {
const ref = useRef<HTMLSpanElement>(null);

useEffect(() => {
//@ts-expect-error - MathJax might exist!
window.MathJax?.typesetPromise?.([ref.current]);
}, []);

return (
<span ref={ref}>
{/* @ts-expect-error - Math is not a valid HTML element according to react */}
<math {...props} dangerouslySetInnerHTML={{ __html: mathContent }} />
</span>
Comment on lines +24 to +27
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Dette legger en span rundt matten. Har det noen praktisk betydning? Ser ut til å fungerer som det skal.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hadde overrasket meg om det hadde noe å si. Tror aldri vi leter etter matte-elementer med css.

);
};
1 change: 1 addition & 0 deletions packages/ndla-ui/src/Embed/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ export type { HeartButtonType, CanonicalUrlFuncs, RenderContext } from "./types"
export { EmbedWrapper } from "./EmbedWrapper";
export type { EmbedWrapperVariantProps, EmbedWrapperProps } from "./EmbedWrapper";
export { ConceptInlineTriggerButton } from "./ConceptInlineTriggerButton";
export { MathEmbed } from "./MathEmbed";
1 change: 1 addition & 0 deletions packages/ndla-ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export {
InlineTriggerButton,
ConceptInlineTriggerButton,
EmbedWrapper,
MathEmbed,
} from "./Embed";

export type { EmbedWrapperProps, EmbedWrapperVariantProps } from "./Embed";
Expand Down