-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduces slots #356
Introduces slots #356
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ import Node from './ast/node'; | |
import transforms from './transforms/index'; | ||
import { OPEN } from './utils'; | ||
|
||
import type { AttributeValue } from './types'; | ||
import type { AttributeValue, ParserArgs } from './types'; | ||
|
||
import type Token from 'markdown-it/lib/token'; | ||
|
||
|
@@ -88,6 +88,7 @@ function handleToken( | |
token: Token, | ||
nodes: Node[], | ||
file?: string, | ||
handleSlots?: boolean, | ||
inlineParent?: Node | ||
) { | ||
if (token.type === 'frontmatter') { | ||
|
@@ -155,7 +156,14 @@ function handleToken( | |
if (attributes && ['tag', 'fence', 'image'].includes(typeName)) | ||
annotate(node, attributes); | ||
|
||
parent.push(node); | ||
if ( | ||
handleSlots && | ||
tag === 'slot' && | ||
typeof node.attributes.primary === 'string' | ||
) | ||
parent.slots[node.attributes.primary] = node; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is necessary. In the event that someone uses This is already the behavior we have today with attributes and we don't enforce any restrictions to prevent that. React itself does pretty much the same thing if use a "children" attribute in JSX as well as passing in actual children. |
||
else parent.push(node); | ||
|
||
if (token.nesting > 0) nodes.push(node); | ||
|
||
if (!Array.isArray(token.children)) return; | ||
|
@@ -167,17 +175,20 @@ function handleToken( | |
const isLeafNode = typeName === 'image'; | ||
if (!isLeafNode) { | ||
for (const child of token.children) | ||
handleToken(child, nodes, file, inlineParent); | ||
handleToken(child, nodes, file, handleSlots, inlineParent); | ||
} | ||
|
||
nodes.pop(); | ||
} | ||
|
||
export default function parser(tokens: Token[], file?: string) { | ||
export default function parser(tokens: Token[], args?: string | ParserArgs) { | ||
mfix-stripe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const doc = new Node('document'); | ||
const nodes = [doc]; | ||
|
||
for (const token of tokens) handleToken(token, nodes, file); | ||
if (typeof args === 'string') args = { file: args }; | ||
|
||
for (const token of tokens) | ||
handleToken(token, nodes, args?.file, args?.slots); | ||
mfix-stripe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (nodes.length > 1) | ||
for (const node of nodes.slice(1)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be faster to make
this.slots
default toundefined
and avoid theObject.values(this.slots)
in the default case?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess alternatively you could simplify this function by doing:
or something similar
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm changing it to:
for (const child of [...Object.values(this.slots), ...this.children]) {
. I don't think it materially impacts performance either way.