safe-mdx 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -4
- package/dist/plugins.d.ts +12 -0
- package/dist/plugins.d.ts.map +1 -0
- package/dist/plugins.js +68 -0
- package/dist/plugins.js.map +1 -0
- package/dist/safe-mdx.d.ts +10 -9
- package/dist/safe-mdx.d.ts.map +1 -1
- package/dist/safe-mdx.js +381 -70
- package/dist/safe-mdx.js.map +1 -1
- package/dist/safe-mdx.test.js +182 -20
- package/dist/safe-mdx.test.js.map +1 -1
- package/package.json +54 -51
- package/src/plugins.ts +87 -0
- package/src/safe-mdx.test.tsx +190 -20
- package/src/safe-mdx.tsx +367 -56
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -2
- package/dist/index.js.map +0 -1
- package/src/index.ts +0 -1
package/src/safe-mdx.tsx
CHANGED
|
@@ -1,22 +1,34 @@
|
|
|
1
1
|
import React, { cloneElement } from 'react'
|
|
2
2
|
import { htmlToJsx } from 'html-to-jsx-transform'
|
|
3
|
-
import { Node, Parent } from 'mdast'
|
|
3
|
+
import { Node, Parent, RootContent } from 'mdast'
|
|
4
4
|
import remarkFrontmatter from 'remark-frontmatter'
|
|
5
5
|
|
|
6
|
-
import { Root,
|
|
6
|
+
import { Root, Yaml } from 'mdast'
|
|
7
7
|
import { MdxJsxFlowElement, MdxJsxTextElement } from 'mdast-util-mdx-jsx'
|
|
8
8
|
import { remark } from 'remark'
|
|
9
9
|
import remarkGfm from 'remark-gfm'
|
|
10
10
|
import remarkMdx from 'remark-mdx'
|
|
11
11
|
|
|
12
12
|
import { Fragment, ReactNode } from 'react'
|
|
13
|
+
import { remarkMarkAndUnravel } from './plugins.js'
|
|
13
14
|
|
|
14
15
|
type MyRootContent = RootContent | Root
|
|
15
16
|
|
|
16
|
-
const
|
|
17
|
+
const processor = remark()
|
|
18
|
+
.use(remarkMdx)
|
|
19
|
+
.use(remarkMarkAndUnravel)
|
|
17
20
|
.use(remarkFrontmatter, ['yaml', 'toml'])
|
|
18
21
|
.use(remarkGfm)
|
|
19
|
-
.use(
|
|
22
|
+
.use(() => {
|
|
23
|
+
return (tree, file) => {
|
|
24
|
+
file.data.ast = tree
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
export function mdxParse(code: string) {
|
|
29
|
+
const file = processor.processSync(code)
|
|
30
|
+
return file.data.ast as Root
|
|
31
|
+
}
|
|
20
32
|
|
|
21
33
|
void React
|
|
22
34
|
|
|
@@ -30,54 +42,6 @@ export function SafeMdxRenderer({
|
|
|
30
42
|
return result
|
|
31
43
|
}
|
|
32
44
|
|
|
33
|
-
const nativeTags = [
|
|
34
|
-
'blockquote',
|
|
35
|
-
'strong',
|
|
36
|
-
'em',
|
|
37
|
-
'del',
|
|
38
|
-
'hr',
|
|
39
|
-
'a',
|
|
40
|
-
'b',
|
|
41
|
-
'br',
|
|
42
|
-
'button',
|
|
43
|
-
'div',
|
|
44
|
-
'form',
|
|
45
|
-
'h1',
|
|
46
|
-
'h2',
|
|
47
|
-
'h3',
|
|
48
|
-
'h4',
|
|
49
|
-
'head',
|
|
50
|
-
'iframe',
|
|
51
|
-
'img',
|
|
52
|
-
'input',
|
|
53
|
-
'label',
|
|
54
|
-
'li',
|
|
55
|
-
'link',
|
|
56
|
-
'ol',
|
|
57
|
-
'p',
|
|
58
|
-
'path',
|
|
59
|
-
'picture',
|
|
60
|
-
'script',
|
|
61
|
-
'section',
|
|
62
|
-
'source',
|
|
63
|
-
'span',
|
|
64
|
-
'sub',
|
|
65
|
-
'sup',
|
|
66
|
-
'svg',
|
|
67
|
-
'table',
|
|
68
|
-
'tbody',
|
|
69
|
-
'td',
|
|
70
|
-
'th',
|
|
71
|
-
'thead',
|
|
72
|
-
'tr',
|
|
73
|
-
'ul',
|
|
74
|
-
'video',
|
|
75
|
-
'code',
|
|
76
|
-
'pre',
|
|
77
|
-
] as const
|
|
78
|
-
|
|
79
|
-
type ComponentsMap = { [k in (typeof nativeTags)[number]]?: any }
|
|
80
|
-
|
|
81
45
|
export class MdastToJsx {
|
|
82
46
|
mdast: MyRootContent
|
|
83
47
|
str: string
|
|
@@ -90,7 +54,8 @@ export class MdastToJsx {
|
|
|
90
54
|
components = {} as ComponentsMap,
|
|
91
55
|
}) {
|
|
92
56
|
this.str = code
|
|
93
|
-
this.mdast = mdast ||
|
|
57
|
+
this.mdast = mdast || mdxParse(code)
|
|
58
|
+
|
|
94
59
|
this.c = {
|
|
95
60
|
...Object.fromEntries(
|
|
96
61
|
nativeTags.map((tag) => {
|
|
@@ -228,7 +193,7 @@ export class MdastToJsx {
|
|
|
228
193
|
case 'heading': {
|
|
229
194
|
const level = node.depth
|
|
230
195
|
|
|
231
|
-
const Tag
|
|
196
|
+
const Tag = this.c[`h${level}`] ?? `h${level}`
|
|
232
197
|
return <Tag>{this.mapMdastChildren(node)}</Tag>
|
|
233
198
|
}
|
|
234
199
|
case 'paragraph': {
|
|
@@ -250,11 +215,27 @@ export class MdastToJsx {
|
|
|
250
215
|
}
|
|
251
216
|
const language = node.lang || ''
|
|
252
217
|
const code = node.value
|
|
253
|
-
|
|
218
|
+
const codeBlock = (className?: string) => (
|
|
254
219
|
<this.c.pre>
|
|
255
|
-
<this.c.code>{code}</this.c.code>
|
|
220
|
+
<this.c.code className={className}>{code}</this.c.code>
|
|
256
221
|
</this.c.pre>
|
|
257
222
|
)
|
|
223
|
+
|
|
224
|
+
if (language) {
|
|
225
|
+
if (
|
|
226
|
+
supportedLanguagesSet.has(
|
|
227
|
+
language as (typeof supportedLanguages)[number],
|
|
228
|
+
)
|
|
229
|
+
) {
|
|
230
|
+
return codeBlock(`language-${language}`)
|
|
231
|
+
} else {
|
|
232
|
+
this.errors.push({
|
|
233
|
+
message: `Unsupported language ${language}`,
|
|
234
|
+
})
|
|
235
|
+
return codeBlock()
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return codeBlock()
|
|
258
239
|
}
|
|
259
240
|
|
|
260
241
|
case 'list': {
|
|
@@ -526,3 +507,333 @@ function safeJsonParse(str: string) {
|
|
|
526
507
|
return null
|
|
527
508
|
}
|
|
528
509
|
}
|
|
510
|
+
|
|
511
|
+
const nativeTags = [
|
|
512
|
+
'blockquote',
|
|
513
|
+
'strong',
|
|
514
|
+
'em',
|
|
515
|
+
'del',
|
|
516
|
+
'hr',
|
|
517
|
+
'a',
|
|
518
|
+
'b',
|
|
519
|
+
'br',
|
|
520
|
+
'button',
|
|
521
|
+
'div',
|
|
522
|
+
'form',
|
|
523
|
+
'h1',
|
|
524
|
+
'h2',
|
|
525
|
+
'h3',
|
|
526
|
+
'h4',
|
|
527
|
+
'head',
|
|
528
|
+
'iframe',
|
|
529
|
+
'img',
|
|
530
|
+
'input',
|
|
531
|
+
'label',
|
|
532
|
+
'li',
|
|
533
|
+
'link',
|
|
534
|
+
'ol',
|
|
535
|
+
'p',
|
|
536
|
+
'path',
|
|
537
|
+
'picture',
|
|
538
|
+
'script',
|
|
539
|
+
'section',
|
|
540
|
+
'source',
|
|
541
|
+
'span',
|
|
542
|
+
'sub',
|
|
543
|
+
'sup',
|
|
544
|
+
'svg',
|
|
545
|
+
'table',
|
|
546
|
+
'tbody',
|
|
547
|
+
'td',
|
|
548
|
+
'tfoot',
|
|
549
|
+
'th',
|
|
550
|
+
'thead',
|
|
551
|
+
'tr',
|
|
552
|
+
'ul',
|
|
553
|
+
'video',
|
|
554
|
+
'code',
|
|
555
|
+
'pre',
|
|
556
|
+
] as const
|
|
557
|
+
|
|
558
|
+
const supportedLanguages = [
|
|
559
|
+
'abap',
|
|
560
|
+
'abnf',
|
|
561
|
+
'actionscript',
|
|
562
|
+
'ada',
|
|
563
|
+
'agda',
|
|
564
|
+
'al',
|
|
565
|
+
'antlr4',
|
|
566
|
+
'apacheconf',
|
|
567
|
+
'apex',
|
|
568
|
+
'apl',
|
|
569
|
+
'applescript',
|
|
570
|
+
'aql',
|
|
571
|
+
'arduino',
|
|
572
|
+
'arff',
|
|
573
|
+
'asciidoc',
|
|
574
|
+
'asm6502',
|
|
575
|
+
'asmatmel',
|
|
576
|
+
'aspnet',
|
|
577
|
+
'autohotkey',
|
|
578
|
+
'autoit',
|
|
579
|
+
'avisynth',
|
|
580
|
+
'avro-idl',
|
|
581
|
+
'bash',
|
|
582
|
+
'basic',
|
|
583
|
+
'batch',
|
|
584
|
+
'bbcode',
|
|
585
|
+
'bicep',
|
|
586
|
+
'birb',
|
|
587
|
+
'bison',
|
|
588
|
+
'bnf',
|
|
589
|
+
'brainfuck',
|
|
590
|
+
'brightscript',
|
|
591
|
+
'bro',
|
|
592
|
+
'bsl',
|
|
593
|
+
'c',
|
|
594
|
+
'cfscript',
|
|
595
|
+
'chaiscript',
|
|
596
|
+
'cil',
|
|
597
|
+
'clike',
|
|
598
|
+
'clojure',
|
|
599
|
+
'cmake',
|
|
600
|
+
'cobol',
|
|
601
|
+
'coffeescript',
|
|
602
|
+
'concurnas',
|
|
603
|
+
'coq',
|
|
604
|
+
'cpp',
|
|
605
|
+
'crystal',
|
|
606
|
+
'csharp',
|
|
607
|
+
'cshtml',
|
|
608
|
+
'csp',
|
|
609
|
+
'css-extras',
|
|
610
|
+
'css',
|
|
611
|
+
'csv',
|
|
612
|
+
'cypher',
|
|
613
|
+
'd',
|
|
614
|
+
'dart',
|
|
615
|
+
'dataweave',
|
|
616
|
+
'dax',
|
|
617
|
+
'dhall',
|
|
618
|
+
'diff',
|
|
619
|
+
'django',
|
|
620
|
+
'dns-zone-file',
|
|
621
|
+
'docker',
|
|
622
|
+
'dot',
|
|
623
|
+
'ebnf',
|
|
624
|
+
'editorconfig',
|
|
625
|
+
'eiffel',
|
|
626
|
+
'ejs',
|
|
627
|
+
'elixir',
|
|
628
|
+
'elm',
|
|
629
|
+
'erb',
|
|
630
|
+
'erlang',
|
|
631
|
+
'etlua',
|
|
632
|
+
'excel-formula',
|
|
633
|
+
'factor',
|
|
634
|
+
'false',
|
|
635
|
+
'firestore-security-rules',
|
|
636
|
+
'flow',
|
|
637
|
+
'fortran',
|
|
638
|
+
'fsharp',
|
|
639
|
+
'ftl',
|
|
640
|
+
'gap',
|
|
641
|
+
'gcode',
|
|
642
|
+
'gdscript',
|
|
643
|
+
'gedcom',
|
|
644
|
+
'gherkin',
|
|
645
|
+
'git',
|
|
646
|
+
'glsl',
|
|
647
|
+
'gml',
|
|
648
|
+
'gn',
|
|
649
|
+
'go-module',
|
|
650
|
+
'go',
|
|
651
|
+
'graphql',
|
|
652
|
+
'groovy',
|
|
653
|
+
'haml',
|
|
654
|
+
'handlebars',
|
|
655
|
+
'haskell',
|
|
656
|
+
'haxe',
|
|
657
|
+
'hcl',
|
|
658
|
+
'hlsl',
|
|
659
|
+
'hoon',
|
|
660
|
+
'hpkp',
|
|
661
|
+
'hsts',
|
|
662
|
+
'http',
|
|
663
|
+
'ichigojam',
|
|
664
|
+
'icon',
|
|
665
|
+
'icu-message-format',
|
|
666
|
+
'idris',
|
|
667
|
+
'iecst',
|
|
668
|
+
'ignore',
|
|
669
|
+
'inform7',
|
|
670
|
+
'ini',
|
|
671
|
+
'io',
|
|
672
|
+
'j',
|
|
673
|
+
'java',
|
|
674
|
+
'javadoc',
|
|
675
|
+
'javadoclike',
|
|
676
|
+
'javascript',
|
|
677
|
+
'javastacktrace',
|
|
678
|
+
'jexl',
|
|
679
|
+
'jolie',
|
|
680
|
+
'jq',
|
|
681
|
+
'js-extras',
|
|
682
|
+
'js-templates',
|
|
683
|
+
'jsdoc',
|
|
684
|
+
'json',
|
|
685
|
+
'json5',
|
|
686
|
+
'jsonp',
|
|
687
|
+
'jsstacktrace',
|
|
688
|
+
'jsx',
|
|
689
|
+
'julia',
|
|
690
|
+
'keepalived',
|
|
691
|
+
'keyman',
|
|
692
|
+
'kotlin',
|
|
693
|
+
'kumir',
|
|
694
|
+
'kusto',
|
|
695
|
+
'latex',
|
|
696
|
+
'latte',
|
|
697
|
+
'less',
|
|
698
|
+
'lilypond',
|
|
699
|
+
'liquid',
|
|
700
|
+
'lisp',
|
|
701
|
+
'livescript',
|
|
702
|
+
'llvm',
|
|
703
|
+
'log',
|
|
704
|
+
'lolcode',
|
|
705
|
+
'lua',
|
|
706
|
+
'magma',
|
|
707
|
+
'makefile',
|
|
708
|
+
'markdown',
|
|
709
|
+
'markup-templating',
|
|
710
|
+
'markup',
|
|
711
|
+
'matlab',
|
|
712
|
+
'maxscript',
|
|
713
|
+
'mel',
|
|
714
|
+
'mermaid',
|
|
715
|
+
'mizar',
|
|
716
|
+
'mongodb',
|
|
717
|
+
'monkey',
|
|
718
|
+
'moonscript',
|
|
719
|
+
'n1ql',
|
|
720
|
+
'n4js',
|
|
721
|
+
'nand2tetris-hdl',
|
|
722
|
+
'naniscript',
|
|
723
|
+
'nasm',
|
|
724
|
+
'neon',
|
|
725
|
+
'nevod',
|
|
726
|
+
'nginx',
|
|
727
|
+
'nim',
|
|
728
|
+
'nix',
|
|
729
|
+
'nsis',
|
|
730
|
+
'objectivec',
|
|
731
|
+
'ocaml',
|
|
732
|
+
'opencl',
|
|
733
|
+
'openqasm',
|
|
734
|
+
'oz',
|
|
735
|
+
'parigp',
|
|
736
|
+
'parser',
|
|
737
|
+
'pascal',
|
|
738
|
+
'pascaligo',
|
|
739
|
+
'pcaxis',
|
|
740
|
+
'peoplecode',
|
|
741
|
+
'perl',
|
|
742
|
+
'php-extras',
|
|
743
|
+
'php',
|
|
744
|
+
'phpdoc',
|
|
745
|
+
'plsql',
|
|
746
|
+
'powerquery',
|
|
747
|
+
'powershell',
|
|
748
|
+
'processing',
|
|
749
|
+
'prolog',
|
|
750
|
+
'promql',
|
|
751
|
+
'properties',
|
|
752
|
+
'protobuf',
|
|
753
|
+
'psl',
|
|
754
|
+
'pug',
|
|
755
|
+
'puppet',
|
|
756
|
+
'pure',
|
|
757
|
+
'purebasic',
|
|
758
|
+
'purescript',
|
|
759
|
+
'python',
|
|
760
|
+
'q',
|
|
761
|
+
'qml',
|
|
762
|
+
'qore',
|
|
763
|
+
'qsharp',
|
|
764
|
+
'r',
|
|
765
|
+
'racket',
|
|
766
|
+
'reason',
|
|
767
|
+
'regex',
|
|
768
|
+
'rego',
|
|
769
|
+
'renpy',
|
|
770
|
+
'rest',
|
|
771
|
+
'rip',
|
|
772
|
+
'roboconf',
|
|
773
|
+
'robotframework',
|
|
774
|
+
'ruby',
|
|
775
|
+
'rust',
|
|
776
|
+
'sas',
|
|
777
|
+
'sass',
|
|
778
|
+
'scala',
|
|
779
|
+
'scheme',
|
|
780
|
+
'scss',
|
|
781
|
+
'shell-session',
|
|
782
|
+
'smali',
|
|
783
|
+
'smalltalk',
|
|
784
|
+
'smarty',
|
|
785
|
+
'sml',
|
|
786
|
+
'solidity',
|
|
787
|
+
'solution-file',
|
|
788
|
+
'soy',
|
|
789
|
+
'sparql',
|
|
790
|
+
'splunk-spl',
|
|
791
|
+
'sqf',
|
|
792
|
+
'sql',
|
|
793
|
+
'squirrel',
|
|
794
|
+
'stan',
|
|
795
|
+
'stylus',
|
|
796
|
+
'swift',
|
|
797
|
+
'systemd',
|
|
798
|
+
't4-cs',
|
|
799
|
+
't4-templating',
|
|
800
|
+
't4-vb',
|
|
801
|
+
'tap',
|
|
802
|
+
'tcl',
|
|
803
|
+
'textile',
|
|
804
|
+
'toml',
|
|
805
|
+
'tremor',
|
|
806
|
+
'tsx',
|
|
807
|
+
'tt2',
|
|
808
|
+
'turtle',
|
|
809
|
+
'twig',
|
|
810
|
+
'typescript',
|
|
811
|
+
'typoscript',
|
|
812
|
+
'unrealscript',
|
|
813
|
+
'uorazor',
|
|
814
|
+
'uri',
|
|
815
|
+
'v',
|
|
816
|
+
'vala',
|
|
817
|
+
'vbnet',
|
|
818
|
+
'velocity',
|
|
819
|
+
'verilog',
|
|
820
|
+
'vhdl',
|
|
821
|
+
'vim',
|
|
822
|
+
'visual-basic',
|
|
823
|
+
'warpscript',
|
|
824
|
+
'wasm',
|
|
825
|
+
'web-idl',
|
|
826
|
+
'wiki',
|
|
827
|
+
'wolfram',
|
|
828
|
+
'wren',
|
|
829
|
+
'xeora',
|
|
830
|
+
'xml-doc',
|
|
831
|
+
'xojo',
|
|
832
|
+
'xquery',
|
|
833
|
+
'yaml',
|
|
834
|
+
'yang',
|
|
835
|
+
'zig',
|
|
836
|
+
] as const
|
|
837
|
+
const supportedLanguagesSet = new Set(supportedLanguages)
|
|
838
|
+
|
|
839
|
+
type ComponentsMap = { [k in (typeof nativeTags)[number]]?: any }
|
package/dist/index.d.ts
DELETED
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA"}
|
package/dist/index.js
DELETED
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA"}
|
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './safe-mdx'
|