Skip to content

Commit

Permalink
refactor: rename content to children
Browse files Browse the repository at this point in the history
  • Loading branch information
quantizor committed Jan 1, 2024
1 parent c20733a commit 20c7a0b
Showing 1 changed file with 45 additions and 43 deletions.
88 changes: 45 additions & 43 deletions index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -902,14 +902,14 @@ function unescapeUrl(rawUrlString: string): string {
*/
function parseInline(
parse: MarkdownToJSX.NestedParser,
content: string,
children: string,
state: MarkdownToJSX.State
): MarkdownToJSX.ParserResult[] {
const isCurrentlyInline = state.inline || false
const isCurrentlySimple = state.simple || false
state.inline = true
state.simple = true
const result = parse(content, state)
const result = parse(children, state)
state.inline = isCurrentlyInline
state.simple = isCurrentlySimple
return result
Expand All @@ -920,33 +920,33 @@ function parseInline(
*/
function parseSimpleInline(
parse: MarkdownToJSX.NestedParser,
content: string,
children: string,
state: MarkdownToJSX.State
): MarkdownToJSX.ParserResult[] {
const isCurrentlyInline = state.inline || false
const isCurrentlySimple = state.simple || false
state.inline = false
state.simple = true
const result = parse(content, state)
const result = parse(children, state)
state.inline = isCurrentlyInline
state.simple = isCurrentlySimple
return result
}

function parseBlock(
parse,
content,
children,
state: MarkdownToJSX.State
): MarkdownToJSX.ParserResult[] {
state.inline = false
return parse(content, state)
return parse(children, state)
}

const parseCaptureInline: MarkdownToJSX.Parser<{
content: MarkdownToJSX.ParserResult[]
children: MarkdownToJSX.ParserResult[]
}> = (capture, parse, state: MarkdownToJSX.State) => {
return {
content: parseInline(parse, capture[1], state),
children: parseInline(parse, capture[1], state),
}
}

Expand Down Expand Up @@ -1205,15 +1205,17 @@ export function compiler(
order: Priority.HIGH,
parse(capture, parse, state) {
return {
content: parse(
children: parse(
capture[0].replace(BLOCKQUOTE_TRIM_LEFT_MULTILINE_R, ''),
state
),
}
},
render(node, output, state) {
return (
<blockquote key={state.key}>{output(node.content, state)}</blockquote>
<blockquote key={state.key}>
{output(node.children, state)}
</blockquote>
)
},
},
Expand Down Expand Up @@ -1353,7 +1355,7 @@ export function compiler(
order: Priority.HIGH,
parse(capture, parse, state) {
return {
content: parseInline(parse, capture[2], state),
children: parseInline(parse, capture[2], state),
id: options.slugify(capture[2]),
level: capture[1].length as MarkdownToJSX.HeadingNode['level'],
}
Expand All @@ -1362,7 +1364,7 @@ export function compiler(
return h(
`h${node.level}`,
{ id: node.id, key: state.key },
output(node.content, state)
output(node.children, state)
)
},
},
Expand All @@ -1372,7 +1374,7 @@ export function compiler(
order: Priority.MAX,
parse(capture, parse, state) {
return {
content: parseInline(parse, capture[1], state),
children: parseInline(parse, capture[1], state),
level: capture[2] === '=' ? 1 : 2,
type: RuleType.heading,
}
Expand Down Expand Up @@ -1404,7 +1406,7 @@ export function compiler(
tag: noInnerParse ? tagName : capture[1],
} as {
attrs: ReturnType<typeof attrStringToMap>
content?: ReturnType<MarkdownToJSX.NestedParser> | undefined
children?: ReturnType<MarkdownToJSX.NestedParser> | undefined
noInnerParse: Boolean
tag: MarkdownToJSX.HTMLTags
text?: string | undefined
Expand All @@ -1415,7 +1417,7 @@ export function compiler(
if (noInnerParse) {
ast.text = capture[3]
} else {
ast.content = parseFunc(parse, trimmed, state)
ast.children = parseFunc(parse, trimmed, state)
}

/**
Expand All @@ -1429,7 +1431,7 @@ export function compiler(
render(node, output, state) {
return (
<node.tag key={state.key} {...node.attrs}>
{node.text || output(node.content, state)}
{node.text || output(node.children, state)}
</node.tag>
)
},
Expand Down Expand Up @@ -1492,15 +1494,15 @@ export function compiler(
order: Priority.LOW,
parse(capture, parse, state) {
return {
content: parseSimpleInline(parse, capture[1], state),
children: parseSimpleInline(parse, capture[1], state),
target: unescapeUrl(capture[2]),
title: capture[3],
}
},
render(node, output, state) {
return (
<a key={state.key} href={sanitizeUrl(node.target)} title={node.title}>
{output(node.content, state)}
{output(node.children, state)}
</a>
)
},
Expand All @@ -1512,7 +1514,7 @@ export function compiler(
order: Priority.MAX,
parse(capture /*, parse, state*/) {
return {
content: [
children: [
{
text: capture[1],
type: RuleType.text,
Expand All @@ -1534,7 +1536,7 @@ export function compiler(
order: Priority.MAX,
parse(capture /*, parse, state*/) {
return {
content: [
children: [
{
text: capture[1],
type: RuleType.text,
Expand All @@ -1560,7 +1562,7 @@ export function compiler(
}

return {
content: [
children: [
{
text: address.replace('mailto:', ''),
type: RuleType.text,
Expand Down Expand Up @@ -1596,7 +1598,7 @@ export function compiler(
order: Priority.LOW,
parse: parseCaptureInline,
render(node, output, state) {
return <p key={state.key}>{output(node.content, state)}</p>
return <p key={state.key}>{output(node.children, state)}</p>
},
} as MarkdownToJSX.Rule<ReturnType<typeof parseCaptureInline>>,

Expand Down Expand Up @@ -1640,8 +1642,8 @@ export function compiler(
order: Priority.MAX,
parse(capture, parse, state) {
return {
content: parse(capture[1], state),
fallbackContent: parse(
children: parse(capture[1], state),
fallbackChildren: parse(
capture[0].replace(SQUARE_BRACKETS_R, '\\$1'),
state
),
Expand All @@ -1655,10 +1657,10 @@ export function compiler(
href={sanitizeUrl(refs[node.ref].target)}
title={refs[node.ref].title}
>
{output(node.content, state)}
{output(node.children, state)}
</a>
) : (
<span key={state.key}>{output(node.fallbackContent, state)}</span>
<span key={state.key}>{output(node.fallbackChildren, state)}</span>
)
},
},
Expand Down Expand Up @@ -1750,11 +1752,11 @@ export function compiler(
return {
// capture[1] -> the syntax control character
// capture[2] -> inner content
content: parse(capture[2], state),
children: parse(capture[2], state),
}
},
render(node, output, state) {
return <strong key={state.key}>{output(node.content, state)}</strong>
return <strong key={state.key}>{output(node.children, state)}</strong>
},
},

Expand All @@ -1765,11 +1767,11 @@ export function compiler(
return {
// capture[1] -> opening * or _
// capture[2] -> inner content
content: parse(capture[2], state),
children: parse(capture[2], state),
}
},
render(node, output, state) {
return <em key={state.key}>{output(node.content, state)}</em>
return <em key={state.key}>{output(node.children, state)}</em>
},
},

Expand All @@ -1793,7 +1795,7 @@ export function compiler(
order: Priority.LOW,
parse: parseCaptureInline,
render(node, output, state) {
return <mark key={state.key}>{output(node.content, state)}</mark>
return <mark key={state.key}>{output(node.children, state)}</mark>
},
},

Expand All @@ -1802,7 +1804,7 @@ export function compiler(
order: Priority.LOW,
parse: parseCaptureInline,
render(node, output, state) {
return <del key={state.key}>{output(node.content, state)}</del>
return <del key={state.key}>{output(node.children, state)}</del>
},
},
}
Expand Down Expand Up @@ -1922,7 +1924,7 @@ export namespace MarkdownToJSX {
}

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

Expand Down Expand Up @@ -1967,7 +1969,7 @@ export namespace MarkdownToJSX {

export interface HeadingNode {
type: (typeof RuleType)['heading']
content: MarkdownToJSX.ParserResult[]
children: MarkdownToJSX.ParserResult[]
id: string
level: 1 | 2 | 3 | 4 | 5 | 6
}
Expand All @@ -1989,7 +1991,7 @@ export namespace MarkdownToJSX {

export interface LinkNode {
type: (typeof RuleType)['link']
content: MarkdownToJSX.ParserResult[]
children: MarkdownToJSX.ParserResult[]
target: string
title?: string
}
Expand Down Expand Up @@ -2025,7 +2027,7 @@ export namespace MarkdownToJSX {

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

export interface ReferenceNode {
Expand All @@ -2040,8 +2042,8 @@ export namespace MarkdownToJSX {

export interface ReferenceLinkNode {
type: (typeof RuleType)['refLink']
content: MarkdownToJSX.ParserResult[]
fallbackContent: MarkdownToJSX.ParserResult[]
children: MarkdownToJSX.ParserResult[]
fallbackChildren: MarkdownToJSX.ParserResult[]
ref: string
}

Expand All @@ -2066,12 +2068,12 @@ export namespace MarkdownToJSX {

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

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

export interface EscapedTextNode {
Expand All @@ -2080,18 +2082,18 @@ export namespace MarkdownToJSX {

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

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

export interface HTMLNode {
type: (typeof RuleType)['htmlBlock']
attrs: JSX.IntrinsicAttributes
content?: ReturnType<MarkdownToJSX.NestedParser> | undefined
children?: ReturnType<MarkdownToJSX.NestedParser> | undefined
noInnerParse: Boolean
tag: MarkdownToJSX.HTMLTags
text?: string | undefined
Expand Down

0 comments on commit 20c7a0b

Please sign in to comment.