-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from TorstenDittmann/feat-pass-proper-types-to-…
…svelte feat: pass proper types to svelte
- Loading branch information
Showing
9 changed files
with
162 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<script context="module"> | ||
export { default as Addition } from './tags/Addition.svelte'; | ||
export { default as Multiply } from './tags/Multiply.svelte'; | ||
export { default as Types } from './tags/Types.svelte'; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<script lang="ts"> | ||
export let string: string; | ||
export let number: number; | ||
export let boolean: boolean; | ||
</script> | ||
|
||
<b>Types</b> | ||
<p><b>{string}</b> is typeof <b>{typeof string}</b></p> | ||
<p><b>{number}</b> is typeof <b>{typeof number}</b></p> | ||
<p><b>{boolean}</b> is typeof <b>{typeof boolean}</b></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
{% addition a=4 b=4 /%} | ||
|
||
{% multiply a=3 b=8 /%} | ||
|
||
{% types string="lorem ipsum" number=123 boolean=true /%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { RenderableTreeNodes, Tag } from '@markdoc/markdoc'; | ||
import { sanitize_for_svelte } from './transformer'; | ||
import { escape } from 'html-escaper'; | ||
|
||
export function render_html(node: RenderableTreeNodes): string { | ||
/** | ||
* if the node is a string or number, it's a text node. | ||
*/ | ||
if (typeof node === 'string' || typeof node === 'number') { | ||
return sanitize_for_svelte(escape(String(node))); | ||
} | ||
|
||
/** | ||
* if the node is an array, render its items. | ||
*/ | ||
if (Array.isArray(node)) { | ||
return node.map(render_html).join(''); | ||
} | ||
|
||
/** | ||
* if the node is not a Tag, it's invalid. | ||
*/ | ||
if (node === null || typeof node !== 'object' || !Tag.isTag(node)) { | ||
return ''; | ||
} | ||
|
||
const { name, attributes, children = [] } = node; | ||
|
||
if (!name) { | ||
return render_html(children); | ||
} | ||
|
||
const is_svelte = is_svelte_component(node); | ||
|
||
/** | ||
* add attributes to the tag. | ||
*/ | ||
let output = `<${name}`; | ||
for (const [key, value] of Object.entries(attributes ?? {})) { | ||
if (is_svelte) { | ||
output += ` ${key.toLowerCase()}=${generate_svelte_attribute_value( | ||
value, | ||
)}`; | ||
} else { | ||
output += ` ${key.toLowerCase()}="${sanitize_for_svelte( | ||
escape(String(value)), | ||
)}"`; | ||
} | ||
} | ||
output += '>'; | ||
|
||
/** | ||
* if the tag is a void element, it doesn't need a closing tag. | ||
*/ | ||
if (is_void_element(name)) { | ||
return output; | ||
} | ||
|
||
/** | ||
* render the children if present. | ||
*/ | ||
if (children.length) { | ||
output += render_html(children); | ||
} | ||
|
||
/** | ||
* close the tag. | ||
*/ | ||
output += `</${name}>`; | ||
|
||
return output; | ||
} | ||
|
||
function is_void_element(name: string): boolean { | ||
return [ | ||
'area', | ||
'base', | ||
'br', | ||
'col', | ||
'embed', | ||
'hr', | ||
'img', | ||
'input', | ||
'link', | ||
'meta', | ||
'param', | ||
'source', | ||
'track', | ||
'wbr', | ||
].includes(name); | ||
} | ||
|
||
function is_svelte_component(node: RenderableTreeNodes): boolean { | ||
return Tag.isTag(node) && node.name.startsWith('INTERNAL__'); | ||
} | ||
|
||
function generate_svelte_attribute_value(value: unknown): string { | ||
switch (typeof value) { | ||
case 'string': | ||
return `"${sanitize_for_svelte(escape(String(value)))}"`; | ||
case 'number': | ||
case 'boolean': | ||
return `{${String(value)}}`; | ||
case 'object': | ||
return `{${JSON.stringify(value)}}`; | ||
default: | ||
throw new Error(`Invalid attribute value: ${value}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters