Skip to content

Commit

Permalink
feat: use constants for import placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
TorstenDittmann committed Sep 5, 2023
1 parent a084830 commit 88f237f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
4 changes: 4 additions & 0 deletions packages/process/src/constants.ts
Original file line number Diff line number Diff line change
@@ -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`;
3 changes: 2 additions & 1 deletion packages/process/src/renderer.ts
Original file line number Diff line number Diff line change
@@ -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 {
/**
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 17 additions & 9 deletions packages/process/src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,15 +81,15 @@ 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);

/**
* 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,
)}';`;
Expand All @@ -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,
)}';`;
Expand All @@ -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,
)}';`;
Expand Down Expand Up @@ -168,10 +169,10 @@ export function transformer({
* wrap the document in the layout
*/
if (has_layout) {
transformed += `<INTERNAL__LAYOUT`;
transformed += `<${LAYOUT_IMPORT}`;
transformed += has_frontmatter ? ' {...frontmatter}>' : '>';
transformed += code;
transformed += `</INTERNAL__LAYOUT>`;
transformed += `</${LAYOUT_IMPORT}>`;
} else {
transformed += code;
}
Expand Down Expand Up @@ -339,15 +340,22 @@ function get_node_defaults(node_type: NodeType): Partial<Schema> {

function prepare_nodes(
nodes_file: Config['nodes'],
dependencies: string,
): Partial<Record<NodeType, Schema>> {
const nodes: Record<string, Schema> = {};
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),
);
Expand All @@ -368,7 +376,7 @@ function prepare_tags(tags_file: Config['tags']): Record<string, Schema> {
*/
const attributes = get_component_vars(String(value), tags_file);
tags[name.toLowerCase()] = {
render: 'INTERNAL__TAGS.' + name,
render: `${TAGS_IMPORT}.${name}`,
attributes,
};
}
Expand Down

0 comments on commit 88f237f

Please sign in to comment.