Skip to content

Commit

Permalink
refactor: replace RuleType enum with a const (#539) (#594)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
roger-schaer and quantizor committed Aug 18, 2024
1 parent acd970d commit 553a175
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 75 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-knives-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'markdown-to-jsx': patch
---

Replace RuleType enum with an object
Binary file not shown.
11 changes: 8 additions & 3 deletions index.cjs.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import Markdown, { compiler } from './'
Object.assign(Markdown, { compiler })
export default Markdown as typeof Markdown & { compiler: typeof compiler }
import Markdown, { compiler, RuleType } from './index.tsx'

Object.assign(Markdown, { compiler, RuleType })

export default Markdown as typeof Markdown & {
compiler: typeof compiler
RuleType: typeof RuleType
}
146 changes: 74 additions & 72 deletions index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,49 @@ import * as React from 'react'
* Analogous to `node.type`. Please note that the values here may change at any time,
* so do not hard code against the value directly.
*/
export const enum RuleType {
blockQuote = '0',
breakLine = '1',
breakThematic = '2',
codeBlock = '3',
codeFenced = '4',
codeInline = '5',
footnote = '6',
footnoteReference = '7',
gfmTask = '8',
heading = '9',
headingSetext = '10',
export const RuleType = {
blockQuote: '0',
breakLine: '1',
breakThematic: '2',
codeBlock: '3',
codeFenced: '4',
codeInline: '5',
footnote: '6',
footnoteReference: '7',
gfmTask: '8',
heading: '9',
headingSetext: '10',
/** only available if not `disableHTMLParsing` */
htmlBlock = '11',
htmlComment = '12',
htmlBlock: '11',
htmlComment: '12',
/** only available if not `disableHTMLParsing` */
htmlSelfClosing = '13',
image = '14',
link = '15',
htmlSelfClosing: '13',
image: '14',
link: '15',
/** emits a `link` 'node', does not render directly */
linkAngleBraceStyleDetector = '16',
linkAngleBraceStyleDetector: '16',
/** emits a `link` 'node', does not render directly */
linkBareUrlDetector = '17',
linkBareUrlDetector: '17',
/** emits a `link` 'node', does not render directly */
linkMailtoDetector = '18',
newlineCoalescer = '19',
orderedList = '20',
paragraph = '21',
ref = '22',
refImage = '23',
refLink = '24',
table = '25',
tableSeparator = '26',
text = '27',
textBolded = '28',
textEmphasized = '29',
textEscaped = '30',
textMarked = '31',
textStrikethroughed = '32',
unorderedList = '33',
}
linkMailtoDetector: '18',
newlineCoalescer: '19',
orderedList: '20',
paragraph: '21',
ref: '22',
refImage: '23',
refLink: '24',
table: '25',
tableSeparator: '26',
text: '27',
textBolded: '28',
textEmphasized: '29',
textEscaped: '30',
textMarked: '31',
textStrikethroughed: '32',
unorderedList: '33',
} as const

export type RuleType = (typeof RuleType)[keyof typeof RuleType]

const enum Priority {
/**
Expand Down Expand Up @@ -2001,130 +2003,130 @@ export namespace MarkdownToJSX {

export interface BlockQuoteNode {
children: MarkdownToJSX.ParserResult[]
type: RuleType.blockQuote
type: typeof RuleType.blockQuote
}

export interface BreakLineNode {
type: RuleType.breakLine
type: typeof RuleType.breakLine
}

export interface BreakThematicNode {
type: RuleType.breakThematic
type: typeof RuleType.breakThematic
}

export interface CodeBlockNode {
type: RuleType.codeBlock
type: typeof RuleType.codeBlock
attrs?: JSX.IntrinsicAttributes
lang?: string
text: string
}

export interface CodeFencedNode {
type: RuleType.codeFenced
type: typeof RuleType.codeFenced
}

export interface CodeInlineNode {
type: RuleType.codeInline
type: typeof RuleType.codeInline
text: string
}

export interface FootnoteNode {
type: RuleType.footnote
type: typeof RuleType.footnote
}

export interface FootnoteReferenceNode {
type: RuleType.footnoteReference
type: typeof RuleType.footnoteReference
target: string
text: string
}

export interface GFMTaskNode {
type: RuleType.gfmTask
type: typeof RuleType.gfmTask
completed: boolean
}

export interface HeadingNode {
type: RuleType.heading
type: typeof RuleType.heading
children: MarkdownToJSX.ParserResult[]
id: string
level: 1 | 2 | 3 | 4 | 5 | 6
}

export interface HeadingSetextNode {
type: RuleType.headingSetext
type: typeof RuleType.headingSetext
}

export interface HTMLCommentNode {
type: RuleType.htmlComment
type: typeof RuleType.htmlComment
}

export interface ImageNode {
type: RuleType.image
type: typeof RuleType.image
alt?: string
target: string
title?: string
}

export interface LinkNode {
type: RuleType.link
type: typeof RuleType.link
children: MarkdownToJSX.ParserResult[]
target: string
title?: string
}

export interface LinkAngleBraceNode {
type: RuleType.linkAngleBraceStyleDetector
type: typeof RuleType.linkAngleBraceStyleDetector
}

export interface LinkBareURLNode {
type: RuleType.linkBareUrlDetector
type: typeof RuleType.linkBareUrlDetector
}

export interface LinkMailtoNode {
type: RuleType.linkMailtoDetector
type: typeof RuleType.linkMailtoDetector
}

export interface OrderedListNode {
type: RuleType.orderedList
type: typeof RuleType.orderedList
items: MarkdownToJSX.ParserResult[][]
ordered: true
start?: number
}

export interface UnorderedListNode {
type: RuleType.unorderedList
type: typeof RuleType.unorderedList
items: MarkdownToJSX.ParserResult[][]
ordered: false
}

export interface NewlineNode {
type: RuleType.newlineCoalescer
type: typeof RuleType.newlineCoalescer
}

export interface ParagraphNode {
type: RuleType.paragraph
type: typeof RuleType.paragraph
children: MarkdownToJSX.ParserResult[]
}

export interface ReferenceNode {
type: RuleType.ref
type: typeof RuleType.ref
}

export interface ReferenceImageNode {
type: RuleType.refImage
type: typeof RuleType.refImage
alt?: string
ref: string
}

export interface ReferenceLinkNode {
type: RuleType.refLink
type: typeof RuleType.refLink
children: MarkdownToJSX.ParserResult[]
fallbackChildren: MarkdownToJSX.ParserResult[]
ref: string
}

export interface TableNode {
type: RuleType.table
type: typeof RuleType.table
/**
* alignment for each table column
*/
Expand All @@ -2134,40 +2136,40 @@ export namespace MarkdownToJSX {
}

export interface TableSeparatorNode {
type: RuleType.tableSeparator
type: typeof RuleType.tableSeparator
}

export interface TextNode {
type: RuleType.text
type: typeof RuleType.text
text: string
}

export interface BoldTextNode {
type: RuleType.textBolded
type: typeof RuleType.textBolded
children: MarkdownToJSX.ParserResult[]
}

export interface ItalicTextNode {
type: RuleType.textEmphasized
type: typeof RuleType.textEmphasized
children: MarkdownToJSX.ParserResult[]
}

export interface EscapedTextNode {
type: RuleType.textEscaped
type: typeof RuleType.textEscaped
}

export interface MarkedTextNode {
type: RuleType.textMarked
type: typeof RuleType.textMarked
children: MarkdownToJSX.ParserResult[]
}

export interface StrikethroughTextNode {
type: RuleType.textStrikethroughed
type: typeof RuleType.textStrikethroughed
children: MarkdownToJSX.ParserResult[]
}

export interface HTMLNode {
type: RuleType.htmlBlock
type: typeof RuleType.htmlBlock
attrs: JSX.IntrinsicAttributes
children?: ReturnType<MarkdownToJSX.NestedParser> | undefined
noInnerParse: Boolean
Expand All @@ -2176,7 +2178,7 @@ export namespace MarkdownToJSX {
}

export interface HTMLSelfClosingNode {
type: RuleType.htmlSelfClosing
type: typeof RuleType.htmlSelfClosing
attrs: JSX.IntrinsicAttributes
tag: string
}
Expand Down Expand Up @@ -2252,8 +2254,8 @@ export namespace MarkdownToJSX {
}

export type Rules = {
[K in ParserResult['type']]: K extends RuleType.table
? Rule<Extract<ParserResult, { type: K | RuleType.paragraph }>>
[K in ParserResult['type']]: K extends typeof RuleType.table
? Rule<Extract<ParserResult, { type: K | typeof RuleType.paragraph }>>
: Rule<Extract<ParserResult, { type: K }>>
}

Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
"compilerOptions": {
"allowImportingTsExtensions": true,
"declaration": true,
// Disable because enabling this option can also force consuming libraries to enable it
"esModuleInterop": false,
"incremental": true,
"jsx": "react",
"module": "ESNext",
"moduleResolution": "node",
"noEmit": true,
"outDir": "./dist",
"preserveConstEnums": true,
"target": "ESNext"
Expand Down

0 comments on commit 553a175

Please sign in to comment.