Skip to content

Commit

Permalink
feat: add custom rule support
Browse files Browse the repository at this point in the history
  • Loading branch information
quantizor committed Apr 25, 2024
1 parent 8cde439 commit 234dcbf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/sour-llamas-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'markdown-to-jsx': minor
---

Add `options.customRules`, this allows for novel functionality to be implemented in tandem with `options.renderRule`.
25 changes: 20 additions & 5 deletions index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const enum RuleType {
unorderedList = '33',
}

const enum Priority {
export const enum Priority {
/**
* anything that must scan the tree before everything else
*/
Expand Down Expand Up @@ -805,7 +805,7 @@ function inlineRegex(regex: RegExp) {
}

// basically any inline element except links
function simpleInlineRegex(regex: RegExp) {
export function simpleInlineRegex(regex: RegExp) {
return function match(source: string, context: MarkdownToJSX.Context) {
if (context.inline || context.simple) {
return regex.exec(source)
Expand All @@ -816,7 +816,7 @@ function simpleInlineRegex(regex: RegExp) {
}

// Creates a match function for a block scoped element from a regex
function blockRegex(regex: RegExp) {
export function blockRegex(regex: RegExp) {
return function match(source: string, context: MarkdownToJSX.Context) {
if (context.inline || context.simple) {
return null
Expand All @@ -827,7 +827,7 @@ function blockRegex(regex: RegExp) {
}

// Creates a match function from a regex, ignoring block/inline scope
function anyScopeRegex(regex: RegExp) {
export function anyScopeRegex(regex: RegExp) {
return function match(source: string /*, context*/) {
return regex.exec(source)
}
Expand Down Expand Up @@ -1936,7 +1936,10 @@ export function createMarkdown(options: MarkdownToJSX.Options = {}) {
options.disabledRules.push(RuleType.htmlBlock, RuleType.htmlSelfClosing)
}

const doParse = parserFor(rules, options)
const doParse = parserFor(
rules.concat(options.customRules as typeof rules),
options
)

/**
* A function that returns AST and resulting state context for given markdown.
Expand Down Expand Up @@ -2368,6 +2371,18 @@ export namespace MarkdownToJSX {
...children: React.ReactNode[]
) => React.ReactChild

/**
* Supply custom rule tuples; this allows for creation and handling of arbitrary syntaxes
* within markdown. Note that `renderRule` needs to be composed as well to intercept the new
* rules you have added and output appropriate JSX.
*
* The key for each rule should be unique and have no overlap with `RuleType`.
*
* For inspiration, it's recommended to see the [source code for this library](https://github.com/quantizor/markdown-to-jsx/blob/main/index.tsx)
* and copy/modify an existing rule to get started.
*/
customRules?: [key: string, rule: MarkdownToJSX.Rule][] | undefined

/**
* Disable the compiler's best-effort transcription of provided raw HTML
* into JSX-equivalent. This is the functionality that prevents the need to
Expand Down

0 comments on commit 234dcbf

Please sign in to comment.