Skip to content

Commit 553a175

Browse files
roger-schaerquantizor
andauthoredAug 18, 2024··
refactor: replace RuleType enum with a const (#539) (#594)
* refactor: replace RuleType enum with a const & export it in index.cjs.tsx * chore: add changeset * chore: fix build --------- Co-authored-by: Evan Jacobs <probablyup@gmail.com>
1 parent acd970d commit 553a175

File tree

5 files changed

+89
-75
lines changed

5 files changed

+89
-75
lines changed
 

‎.changeset/nasty-knives-attack.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'markdown-to-jsx': patch
3+
---
4+
5+
Replace RuleType enum with an object
Binary file not shown.

‎index.cjs.tsx

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
import Markdown, { compiler } from './'
2-
Object.assign(Markdown, { compiler })
3-
export default Markdown as typeof Markdown & { compiler: typeof compiler }
1+
import Markdown, { compiler, RuleType } from './index.tsx'
2+
3+
Object.assign(Markdown, { compiler, RuleType })
4+
5+
export default Markdown as typeof Markdown & {
6+
compiler: typeof compiler
7+
RuleType: typeof RuleType
8+
}

‎index.tsx

+74-72
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,49 @@ import * as React from 'react'
1212
* Analogous to `node.type`. Please note that the values here may change at any time,
1313
* so do not hard code against the value directly.
1414
*/
15-
export const enum RuleType {
16-
blockQuote = '0',
17-
breakLine = '1',
18-
breakThematic = '2',
19-
codeBlock = '3',
20-
codeFenced = '4',
21-
codeInline = '5',
22-
footnote = '6',
23-
footnoteReference = '7',
24-
gfmTask = '8',
25-
heading = '9',
26-
headingSetext = '10',
15+
export const RuleType = {
16+
blockQuote: '0',
17+
breakLine: '1',
18+
breakThematic: '2',
19+
codeBlock: '3',
20+
codeFenced: '4',
21+
codeInline: '5',
22+
footnote: '6',
23+
footnoteReference: '7',
24+
gfmTask: '8',
25+
heading: '9',
26+
headingSetext: '10',
2727
/** only available if not `disableHTMLParsing` */
28-
htmlBlock = '11',
29-
htmlComment = '12',
28+
htmlBlock: '11',
29+
htmlComment: '12',
3030
/** only available if not `disableHTMLParsing` */
31-
htmlSelfClosing = '13',
32-
image = '14',
33-
link = '15',
31+
htmlSelfClosing: '13',
32+
image: '14',
33+
link: '15',
3434
/** emits a `link` 'node', does not render directly */
35-
linkAngleBraceStyleDetector = '16',
35+
linkAngleBraceStyleDetector: '16',
3636
/** emits a `link` 'node', does not render directly */
37-
linkBareUrlDetector = '17',
37+
linkBareUrlDetector: '17',
3838
/** emits a `link` 'node', does not render directly */
39-
linkMailtoDetector = '18',
40-
newlineCoalescer = '19',
41-
orderedList = '20',
42-
paragraph = '21',
43-
ref = '22',
44-
refImage = '23',
45-
refLink = '24',
46-
table = '25',
47-
tableSeparator = '26',
48-
text = '27',
49-
textBolded = '28',
50-
textEmphasized = '29',
51-
textEscaped = '30',
52-
textMarked = '31',
53-
textStrikethroughed = '32',
54-
unorderedList = '33',
55-
}
39+
linkMailtoDetector: '18',
40+
newlineCoalescer: '19',
41+
orderedList: '20',
42+
paragraph: '21',
43+
ref: '22',
44+
refImage: '23',
45+
refLink: '24',
46+
table: '25',
47+
tableSeparator: '26',
48+
text: '27',
49+
textBolded: '28',
50+
textEmphasized: '29',
51+
textEscaped: '30',
52+
textMarked: '31',
53+
textStrikethroughed: '32',
54+
unorderedList: '33',
55+
} as const
56+
57+
export type RuleType = (typeof RuleType)[keyof typeof RuleType]
5658

5759
const enum Priority {
5860
/**
@@ -2001,130 +2003,130 @@ export namespace MarkdownToJSX {
20012003

20022004
export interface BlockQuoteNode {
20032005
children: MarkdownToJSX.ParserResult[]
2004-
type: RuleType.blockQuote
2006+
type: typeof RuleType.blockQuote
20052007
}
20062008

20072009
export interface BreakLineNode {
2008-
type: RuleType.breakLine
2010+
type: typeof RuleType.breakLine
20092011
}
20102012

20112013
export interface BreakThematicNode {
2012-
type: RuleType.breakThematic
2014+
type: typeof RuleType.breakThematic
20132015
}
20142016

20152017
export interface CodeBlockNode {
2016-
type: RuleType.codeBlock
2018+
type: typeof RuleType.codeBlock
20172019
attrs?: JSX.IntrinsicAttributes
20182020
lang?: string
20192021
text: string
20202022
}
20212023

20222024
export interface CodeFencedNode {
2023-
type: RuleType.codeFenced
2025+
type: typeof RuleType.codeFenced
20242026
}
20252027

20262028
export interface CodeInlineNode {
2027-
type: RuleType.codeInline
2029+
type: typeof RuleType.codeInline
20282030
text: string
20292031
}
20302032

20312033
export interface FootnoteNode {
2032-
type: RuleType.footnote
2034+
type: typeof RuleType.footnote
20332035
}
20342036

20352037
export interface FootnoteReferenceNode {
2036-
type: RuleType.footnoteReference
2038+
type: typeof RuleType.footnoteReference
20372039
target: string
20382040
text: string
20392041
}
20402042

20412043
export interface GFMTaskNode {
2042-
type: RuleType.gfmTask
2044+
type: typeof RuleType.gfmTask
20432045
completed: boolean
20442046
}
20452047

20462048
export interface HeadingNode {
2047-
type: RuleType.heading
2049+
type: typeof RuleType.heading
20482050
children: MarkdownToJSX.ParserResult[]
20492051
id: string
20502052
level: 1 | 2 | 3 | 4 | 5 | 6
20512053
}
20522054

20532055
export interface HeadingSetextNode {
2054-
type: RuleType.headingSetext
2056+
type: typeof RuleType.headingSetext
20552057
}
20562058

20572059
export interface HTMLCommentNode {
2058-
type: RuleType.htmlComment
2060+
type: typeof RuleType.htmlComment
20592061
}
20602062

20612063
export interface ImageNode {
2062-
type: RuleType.image
2064+
type: typeof RuleType.image
20632065
alt?: string
20642066
target: string
20652067
title?: string
20662068
}
20672069

20682070
export interface LinkNode {
2069-
type: RuleType.link
2071+
type: typeof RuleType.link
20702072
children: MarkdownToJSX.ParserResult[]
20712073
target: string
20722074
title?: string
20732075
}
20742076

20752077
export interface LinkAngleBraceNode {
2076-
type: RuleType.linkAngleBraceStyleDetector
2078+
type: typeof RuleType.linkAngleBraceStyleDetector
20772079
}
20782080

20792081
export interface LinkBareURLNode {
2080-
type: RuleType.linkBareUrlDetector
2082+
type: typeof RuleType.linkBareUrlDetector
20812083
}
20822084

20832085
export interface LinkMailtoNode {
2084-
type: RuleType.linkMailtoDetector
2086+
type: typeof RuleType.linkMailtoDetector
20852087
}
20862088

20872089
export interface OrderedListNode {
2088-
type: RuleType.orderedList
2090+
type: typeof RuleType.orderedList
20892091
items: MarkdownToJSX.ParserResult[][]
20902092
ordered: true
20912093
start?: number
20922094
}
20932095

20942096
export interface UnorderedListNode {
2095-
type: RuleType.unorderedList
2097+
type: typeof RuleType.unorderedList
20962098
items: MarkdownToJSX.ParserResult[][]
20972099
ordered: false
20982100
}
20992101

21002102
export interface NewlineNode {
2101-
type: RuleType.newlineCoalescer
2103+
type: typeof RuleType.newlineCoalescer
21022104
}
21032105

21042106
export interface ParagraphNode {
2105-
type: RuleType.paragraph
2107+
type: typeof RuleType.paragraph
21062108
children: MarkdownToJSX.ParserResult[]
21072109
}
21082110

21092111
export interface ReferenceNode {
2110-
type: RuleType.ref
2112+
type: typeof RuleType.ref
21112113
}
21122114

21132115
export interface ReferenceImageNode {
2114-
type: RuleType.refImage
2116+
type: typeof RuleType.refImage
21152117
alt?: string
21162118
ref: string
21172119
}
21182120

21192121
export interface ReferenceLinkNode {
2120-
type: RuleType.refLink
2122+
type: typeof RuleType.refLink
21212123
children: MarkdownToJSX.ParserResult[]
21222124
fallbackChildren: MarkdownToJSX.ParserResult[]
21232125
ref: string
21242126
}
21252127

21262128
export interface TableNode {
2127-
type: RuleType.table
2129+
type: typeof RuleType.table
21282130
/**
21292131
* alignment for each table column
21302132
*/
@@ -2134,40 +2136,40 @@ export namespace MarkdownToJSX {
21342136
}
21352137

21362138
export interface TableSeparatorNode {
2137-
type: RuleType.tableSeparator
2139+
type: typeof RuleType.tableSeparator
21382140
}
21392141

21402142
export interface TextNode {
2141-
type: RuleType.text
2143+
type: typeof RuleType.text
21422144
text: string
21432145
}
21442146

21452147
export interface BoldTextNode {
2146-
type: RuleType.textBolded
2148+
type: typeof RuleType.textBolded
21472149
children: MarkdownToJSX.ParserResult[]
21482150
}
21492151

21502152
export interface ItalicTextNode {
2151-
type: RuleType.textEmphasized
2153+
type: typeof RuleType.textEmphasized
21522154
children: MarkdownToJSX.ParserResult[]
21532155
}
21542156

21552157
export interface EscapedTextNode {
2156-
type: RuleType.textEscaped
2158+
type: typeof RuleType.textEscaped
21572159
}
21582160

21592161
export interface MarkedTextNode {
2160-
type: RuleType.textMarked
2162+
type: typeof RuleType.textMarked
21612163
children: MarkdownToJSX.ParserResult[]
21622164
}
21632165

21642166
export interface StrikethroughTextNode {
2165-
type: RuleType.textStrikethroughed
2167+
type: typeof RuleType.textStrikethroughed
21662168
children: MarkdownToJSX.ParserResult[]
21672169
}
21682170

21692171
export interface HTMLNode {
2170-
type: RuleType.htmlBlock
2172+
type: typeof RuleType.htmlBlock
21712173
attrs: JSX.IntrinsicAttributes
21722174
children?: ReturnType<MarkdownToJSX.NestedParser> | undefined
21732175
noInnerParse: Boolean
@@ -2176,7 +2178,7 @@ export namespace MarkdownToJSX {
21762178
}
21772179

21782180
export interface HTMLSelfClosingNode {
2179-
type: RuleType.htmlSelfClosing
2181+
type: typeof RuleType.htmlSelfClosing
21802182
attrs: JSX.IntrinsicAttributes
21812183
tag: string
21822184
}
@@ -2252,8 +2254,8 @@ export namespace MarkdownToJSX {
22522254
}
22532255

22542256
export type Rules = {
2255-
[K in ParserResult['type']]: K extends RuleType.table
2256-
? Rule<Extract<ParserResult, { type: K | RuleType.paragraph }>>
2257+
[K in ParserResult['type']]: K extends typeof RuleType.table
2258+
? Rule<Extract<ParserResult, { type: K | typeof RuleType.paragraph }>>
22572259
: Rule<Extract<ParserResult, { type: K }>>
22582260
}
22592261

‎tsconfig.json

+2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
{
22
"compilerOptions": {
3+
"allowImportingTsExtensions": true,
34
"declaration": true,
45
// Disable because enabling this option can also force consuming libraries to enable it
56
"esModuleInterop": false,
67
"incremental": true,
78
"jsx": "react",
89
"module": "ESNext",
910
"moduleResolution": "node",
11+
"noEmit": true,
1012
"outDir": "./dist",
1113
"preserveConstEnums": true,
1214
"target": "ESNext"

0 commit comments

Comments
 (0)
Please sign in to comment.