From 88f237f9d42459faa96bac06e63dc4ee9db4893f Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Tue, 5 Sep 2023 19:53:34 +0200 Subject: [PATCH] feat: use constants for import placeholders --- packages/process/src/constants.ts | 4 ++++ packages/process/src/renderer.ts | 3 ++- packages/process/src/transformer.ts | 26 +++++++++++++++++--------- 3 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 packages/process/src/constants.ts diff --git a/packages/process/src/constants.ts b/packages/process/src/constants.ts new file mode 100644 index 0000000..bfa63dd --- /dev/null +++ b/packages/process/src/constants.ts @@ -0,0 +1,4 @@ +export const IMPORT_PREFIX = 'INTERNAL__'; +export const TAGS_IMPORT = `${IMPORT_PREFIX}TAGS`; +export const NODES_IMPORT = `${IMPORT_PREFIX}NODES`; +export const LAYOUT_IMPORT = `${IMPORT_PREFIX}LAYOUT`; diff --git a/packages/process/src/renderer.ts b/packages/process/src/renderer.ts index 9673b19..895888e 100644 --- a/packages/process/src/renderer.ts +++ b/packages/process/src/renderer.ts @@ -1,6 +1,7 @@ import { RenderableTreeNodes, Tag } from '@markdoc/markdoc'; import { sanitize_for_svelte } from './transformer'; import { escape } from 'html-escaper'; +import { IMPORT_PREFIX } from './constants'; export function render_html(node: RenderableTreeNodes): string { /** @@ -91,7 +92,7 @@ function is_void_element(name: string): boolean { } function is_svelte_component(node: RenderableTreeNodes): boolean { - return Tag.isTag(node) && node.name.startsWith('INTERNAL__'); + return Tag.isTag(node) && node.name.startsWith(IMPORT_PREFIX); } function generate_svelte_attribute_value(value: unknown): string { diff --git a/packages/process/src/transformer.ts b/packages/process/src/transformer.ts index 0e02615..585f8f6 100644 --- a/packages/process/src/transformer.ts +++ b/packages/process/src/transformer.ts @@ -29,6 +29,7 @@ import { } from './utils'; import * as default_schema from './default_schema'; import type { Config } from './config'; +import { LAYOUT_IMPORT, NODES_IMPORT, TAGS_IMPORT } from './constants'; type Var = { name: string; @@ -80,7 +81,7 @@ export function transformer({ let dependencies = ''; const tags = prepare_tags(tags_file); const has_tags = Object.keys(tags).length > 0; - const nodes = prepare_nodes(nodes_file); + const nodes = prepare_nodes(nodes_file, dependencies); const has_nodes = Object.keys(nodes).length > 0; const partials = prepare_partials(partials_dir); @@ -88,7 +89,7 @@ export function transformer({ * add import for tags */ if (tags_file && has_tags) { - dependencies += `import * as INTERNAL__TAGS from '${relative_posix_path( + dependencies += `import * as ${TAGS_IMPORT} from '${relative_posix_path( filename, tags_file, )}';`; @@ -98,7 +99,7 @@ export function transformer({ * add import for nodes */ if (nodes_file && has_nodes) { - dependencies += `import * as INTERNAL__NODES from '${relative_posix_path( + dependencies += `import * as ${NODES_IMPORT} from '${relative_posix_path( filename, nodes_file, )}';`; @@ -108,7 +109,7 @@ export function transformer({ * add import for layout */ if (selected_layout && has_layout) { - dependencies += `import INTERNAL__LAYOUT from '${relative_posix_path( + dependencies += `import ${LAYOUT_IMPORT} from '${relative_posix_path( filename, selected_layout, )}';`; @@ -168,10 +169,10 @@ export function transformer({ * wrap the document in the layout */ if (has_layout) { - transformed += `' : '>'; transformed += code; - transformed += ``; + transformed += ``; } else { transformed += code; } @@ -339,15 +340,22 @@ function get_node_defaults(node_type: NodeType): Partial { function prepare_nodes( nodes_file: Config['nodes'], + dependencies: string, ): 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(name.toLowerCase() as NodeType), + ...get_node_defaults(type), transform(node, config) { + if (type === 'image') { + node.attributes.src; + } return new Tag( - `INTERNAL__NODES.${name}`, + `${NODES_IMPORT}.${name}`, node.transformAttributes(config), node.transformChildren(config), ); @@ -368,7 +376,7 @@ function prepare_tags(tags_file: Config['tags']): Record { */ const attributes = get_component_vars(String(value), tags_file); tags[name.toLowerCase()] = { - render: 'INTERNAL__TAGS.' + name, + render: `${TAGS_IMPORT}.${name}`, attributes, }; }