From 39f5d33b090383a0a257c74ec02c0b887d5f19ab Mon Sep 17 00:00:00 2001 From: Maxime Cannoodt Date: Sat, 25 Jun 2022 10:25:30 +0200 Subject: [PATCH] inline tokenizer --- server/prisma/dev.db | Bin 139264 -> 143360 bytes webapp/src/lib/components/CalloutIcon.svelte | 2 -- .../lib/components/MarkdownRenderer.svelte | 4 ++- webapp/src/lib/marked/extensions.ts | 30 +++++++++++++++++- .../lib/marked/renderers/MathInline.svelte | 5 +++ 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 webapp/src/lib/marked/renderers/MathInline.svelte diff --git a/server/prisma/dev.db b/server/prisma/dev.db index 204dccd96e5ca92c668d3b9a273e6f52f75526bd..0cc4d75c228c6b205a14a8e1cabff6a2f6d42e3b 100644 GIT binary patch delta 1488 zcmZ{kyRX}H6vyvvRa&^cEr<~zAxa;ThuFX0&-Ty5rAnOmkvMUlj*|i5_cO5*$4=}e zy%faI1yq)f6^MzY>d+Mlu`(dBB4!qb{sW|>$Q_#PM)1SdivIz;>rBoi+7K$7;WMz+qfiv9EUaqwE`u%S;cq$PNRGY8Ir{K zMT&#OLRA*t{wGb=y9G(I^oum!IN07r@Yen351u`LaD4jJwbR207f+6lt`R4{pBx|h zK7V6>?%uh4_tGb)=coVM{rLRRA3uIQ0YYYrTBK7P6OMDP)7j8U%ad0Uy9{(m(`ymv z){#>UPz(DmNRzTFF|nko?N(n%d*f!yDudMmVILQDVZA2v8n*UAUX=t9s61oz0>>}5 zUVBv@_-h-M2n(?bGit|gGhH_!he^(YG(Opube}4I1$?R*yA_{{u&K#qqtC;r;0t0poQEnW$a5}*bXb>VS>WiJ z)G~_@6H&HmZ#~vurPB%LR$4Z_uU< z9f}430bo)bOC#GbNWd|MLplJE6b+GMVhnB8vMhLicOo<0@vDf=zK~I zGHS5t3tfF)8jYUcW_f{?CVj}~L{e+0X%At*RiYLvBi>3UQQWl0l_zlCDASqDpN;#u zfkIDd&KK+5a%<;4y`2>?!SJafBib?cNC)5EIPY^J`l z1G{ {#if icon == 'note'} diff --git a/webapp/src/lib/components/MarkdownRenderer.svelte b/webapp/src/lib/components/MarkdownRenderer.svelte index dd5e6c1..56fb39e 100644 --- a/webapp/src/lib/components/MarkdownRenderer.svelte +++ b/webapp/src/lib/components/MarkdownRenderer.svelte @@ -10,6 +10,7 @@ import Highlight from '$lib/marked/renderers/Highlight.svelte'; import InternalEmbed from '$lib/marked/renderers/InternalEmbed.svelte'; import Blockquote from '$lib/marked/renderers/Blockquote.svelte'; + import MathInline from '$lib/marked/renderers/MathInline.svelte'; export let plaintext: string; @@ -34,7 +35,8 @@ prose-blockquote:first:before:content-['']" 'internal-embed': InternalEmbed, tag: Tag, highlight: Highlight, - blockquote: Blockquote + blockquote: Blockquote, + 'math-inline': MathInline }} source={plaintext} {options} diff --git a/webapp/src/lib/marked/extensions.ts b/webapp/src/lib/marked/extensions.ts index 55620a9..aea20f6 100644 --- a/webapp/src/lib/marked/extensions.ts +++ b/webapp/src/lib/marked/extensions.ts @@ -59,6 +59,7 @@ const HighlightExtension = { name: 'highlight', level: 'inline', start(src: string) { + // console.log(src); return src.match(/==/)?.index; }, tokenizer(src: string) { @@ -74,6 +75,33 @@ const HighlightExtension = { } }; -export default [InternalLinkExtension, InternalEmbedExtension, TagExtension, HighlightExtension]; +const MathInline = { + name: 'math-inline', + level: 'inline', + start(src: string) { + return src.indexOf('$'); + }, + + tokenizer(src: string) { + const match = src.match(/^(\${1})((?:\\.|.)*)\1/); + console.log(src, match); + if (match) { + return { + type: 'math-inline', + raw: match[0], + text: match[2].trim() + }; + } + return false; + } +}; + +export default [ + InternalLinkExtension, + InternalEmbedExtension, + TagExtension, + HighlightExtension, + MathInline +]; // ^\#([\w\/]+)\W* diff --git a/webapp/src/lib/marked/renderers/MathInline.svelte b/webapp/src/lib/marked/renderers/MathInline.svelte new file mode 100644 index 0000000..52dd2e6 --- /dev/null +++ b/webapp/src/lib/marked/renderers/MathInline.svelte @@ -0,0 +1,5 @@ + + +{text}