From e71eba69eff8bd2ba718727d83de637e358b2842 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Fri, 12 Apr 2024 18:42:16 +0200 Subject: [PATCH 1/2] fix: named imports --- packages/process/src/transformer.ts | 61 ++++++++++++++----- .../nodes, tags and partials/compiled.txt | 2 +- .../tests/processor/nodes/compiled.txt | 2 +- .../process/tests/processor/tags/compiled.txt | 2 +- 4 files changed, 49 insertions(+), 18 deletions(-) diff --git a/packages/process/src/transformer.ts b/packages/process/src/transformer.ts index 6217c96..518761c 100644 --- a/packages/process/src/transformer.ts +++ b/packages/process/src/transformer.ts @@ -8,6 +8,7 @@ import { ConfigType, validate, Tokenizer, + Node, } from '@markdoc/markdoc'; import { ScriptTarget, @@ -73,6 +74,7 @@ export function transformer({ * create ast for markdoc */ const ast = markdocParse(tokens); + const [used_nodes, used_tags] = get_all_nodes_and_tags(ast); /** * load frontmatter @@ -103,8 +105,16 @@ export function transformer({ /** * add import for tags */ - if (tags_file && has_tags) { - dependencies += `import * as ${TAGS_IMPORT} from '${relative_posix_path( + if (tags_file && has_tags && used_tags.size > 0) { + const imports = [...used_tags] + .filter((tag) => tag in tags) + .map((tag) => { + const component = tags[tag].component; + + return `${component} as ${TAGS_IMPORT}_${component}`; + }) + .join(', '); + dependencies += `import { ${imports} from '${relative_posix_path( filename, tags_file, )}';`; @@ -113,8 +123,16 @@ export function transformer({ /** * add import for nodes */ - if (nodes_file && has_nodes) { - dependencies += `import * as ${NODES_IMPORT} from '${relative_posix_path( + if (nodes_file && has_nodes && used_nodes.size > 0) { + const imports = [...used_nodes] + .filter((node) => node in nodes) + .map((node) => { + const component = nodes[node]?.component; + + return `${component} as ${NODES_IMPORT}_${component}`; + }) + .join(', '); + dependencies += `import { ${imports} } from '${relative_posix_path( filename, nodes_file, )}';`; @@ -395,23 +413,33 @@ function get_node_defaults(node_type: NodeType): Partial { } } +function get_all_nodes_and_tags(ast: Node): [Set, Set] { + const nodes = new Set(); + const tags = new Set(); + for (const node of ast.walk()) { + if (node.type === 'tag' && node?.tag) { + tags.add(node.tag); + } else { + nodes.add(node.type); + } + } + + return [nodes, tags]; +} + function prepare_nodes( nodes_file: Config['nodes'], -): Partial> { - const nodes: Record = {}; +): Partial> { + const nodes: Record = {}; if (nodes_file) { for (const [name] of each_exported_var(nodes_file)) { const type = name.toLowerCase() as NodeType; - if (type === 'image') { - } nodes[name.toLowerCase()] = { ...get_node_defaults(type), + component: name, transform(node, config) { - if (type === 'image') { - node.attributes.src; - } return new Tag( - `${NODES_IMPORT}.${name}`, + `${NODES_IMPORT}_${name}`, node.transformAttributes(config), node.transformChildren(config), ); @@ -423,8 +451,10 @@ function prepare_nodes( return nodes; } -function prepare_tags(tags_file: Config['tags']): Record { - const tags: Record = {}; +function prepare_tags( + tags_file: Config['tags'], +): Record { + const tags: Record = {}; if (tags_file) { for (const [name, value] of each_exported_var(tags_file)) { /** @@ -432,7 +462,8 @@ function prepare_tags(tags_file: Config['tags']): Record { */ const attributes = get_component_vars(String(value), tags_file); tags[name.toLowerCase()] = { - render: `${TAGS_IMPORT}.${name}`, + render: `${TAGS_IMPORT}_${name}`, + component: name, attributes, }; } diff --git a/packages/process/tests/processor/nodes, tags and partials/compiled.txt b/packages/process/tests/processor/nodes, tags and partials/compiled.txt index 94ac4ab..7ba20ec 100644 --- a/packages/process/tests/processor/nodes, tags and partials/compiled.txt +++ b/packages/process/tests/processor/nodes, tags and partials/compiled.txt @@ -1 +1 @@ -
Heading 1Heading 2With ID With Class

slot content

I am a partialLorem IpsumI am nested
\ No newline at end of file +
Heading 1Heading 2With ID With Class

slot content

I am a partialLorem IpsumI am nested
\ No newline at end of file diff --git a/packages/process/tests/processor/nodes/compiled.txt b/packages/process/tests/processor/nodes/compiled.txt index 5ace9da..7583d3a 100644 --- a/packages/process/tests/processor/nodes/compiled.txt +++ b/packages/process/tests/processor/nodes/compiled.txt @@ -1 +1 @@ -
Heading 1Heading 2With ID With Class
\ No newline at end of file +
Heading 1Heading 2With ID With Class
\ No newline at end of file diff --git a/packages/process/tests/processor/tags/compiled.txt b/packages/process/tests/processor/tags/compiled.txt index 38ac354..c67933f 100644 --- a/packages/process/tests/processor/tags/compiled.txt +++ b/packages/process/tests/processor/tags/compiled.txt @@ -1 +1 @@ -

slot content

\ No newline at end of file +

slot content

\ No newline at end of file From dc317d8589633e61991dfc7abb33fa9008dabb6a Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Fri, 12 Apr 2024 18:44:27 +0200 Subject: [PATCH 2/2] fix: accidentaly removed brace --- packages/process/src/transformer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/process/src/transformer.ts b/packages/process/src/transformer.ts index 518761c..d154901 100644 --- a/packages/process/src/transformer.ts +++ b/packages/process/src/transformer.ts @@ -114,7 +114,7 @@ export function transformer({ return `${component} as ${TAGS_IMPORT}_${component}`; }) .join(', '); - dependencies += `import { ${imports} from '${relative_posix_path( + dependencies += `import { ${imports} } from '${relative_posix_path( filename, tags_file, )}';`;