Add smartypants

main
trivernis 5 months ago
parent 4d10a4db8a
commit 62a3727a8d
Signed by: Trivernis
GPG Key ID: 7E6D18B61C8D2F4B

@ -14,8 +14,10 @@
import TableRow from "../atoms/TableRow.svelte"; import TableRow from "../atoms/TableRow.svelte";
import TableCell from "../atoms/TableCell.svelte"; import TableCell from "../atoms/TableCell.svelte";
import Image from "../atoms/Image.svelte"; import Image from "../atoms/Image.svelte";
import { markedEmoji } from "marked-emoji";
import emojis from "$lib/emojis"; import emojis from "$lib/emojis";
import { smartypants } from "$lib/utils";
import { markedEmoji } from "marked-emoji";
type Props = { type Props = {
markdown: string; markdown: string;
@ -24,6 +26,7 @@
const { markdown }: Props = $props(); const { markdown }: Props = $props();
marked.use(markedEmoji({ emojis })); marked.use(markedEmoji({ emojis }));
const markdownTokens = marked.lexer(markdown); const markdownTokens = marked.lexer(markdown);
</script> </script>
@ -93,7 +96,7 @@
{@render markdownToken(childToken)} {@render markdownToken(childToken)}
{/each} {/each}
{:else if token.text} {:else if token.text}
{token.text} {@html smartypants(token.text)}
{:else}{@html "<!-- This token does not hold value -->"}{/if} {:else}{@html "<!-- This token does not hold value -->"}{/if}
{/snippet} {/snippet}

@ -1,6 +1,7 @@
<script lang="ts"> <script lang="ts">
import type { Token } from "marked"; import type { Token } from "marked";
import HighlightedCode from "../atoms/HighlightedCode.svelte"; import HighlightedCode from "../atoms/HighlightedCode.svelte";
import { smartypants } from "$lib/utils";
type Props = { type Props = {
markdownToken: Token; markdownToken: Token;
@ -35,8 +36,11 @@
{#each token.tokens as childToken} {#each token.tokens as childToken}
{@render inlineMarkdown(childToken)} {@render inlineMarkdown(childToken)}
{/each} {/each}
{:else} {:else if token.type === "codespan"}
<!-- don't smartypants code -->
{@html token.text} {@html token.text}
{:else}
{@html smartypants(token.text)}
{/if} {/if}
{/snippet} {/snippet}

@ -0,0 +1,23 @@
export function smartypants(text: string): string {
const replacements = [
["---", "—"],
[",,", "¸"],
["(c)", "ⓒ"],
["(C)", "Ⓒ"],
["(r)", "ⓡ"],
["(R)", "Ⓡ"],
["(tm)", "™"],
["(TM)", "™"],
["(p)", "ⓟ"],
["(P)", "Ⓟ"],
["+-", "±"],
];
let result = text;
for (const [search, replacement] of replacements) {
result = result.replaceAll(search, replacement);
}
return result;
}
Loading…
Cancel
Save