-
Notifications
You must be signed in to change notification settings - Fork 5
/
createHtmlSvelte.js
80 lines (74 loc) · 1.81 KB
/
createHtmlSvelte.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { writeFileSync } from 'fs';
import { join } from 'path';
import { fileURLToPath } from 'url';
/**
* @param {string} str
* @returns {string}
*/
const upperFirst = (str) => {
const [first, ...rest] = [...str];
return first.toUpperCase() + rest.join('');
};
const dir = join(fileURLToPath(import.meta.url), '../../src/lib/renderer/html');
const tagNamesHaveChildren = [
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'p',
'ul',
'ol',
'li',
'a',
'strong',
'em',
'code',
'pre',
'blockquote'
];
const tagNamesHaveNoChild = ['input', 'hr', 'br'];
const upperFirstTagNames = tagNamesHaveChildren
.map(upperFirst)
.concat(tagNamesHaveNoChild.map(upperFirst));
tagNamesHaveChildren.map((tagName) =>
writeFileSync(
join(dir, `${upperFirst(tagName)}.svelte`),
`<script lang="ts">
import { classNameTransform } from '../../utils';
import type { Node } from '$lib/types';
import Children from '../Children.svelte';
export let children: Node[];
export let properties: Record<string, unknown>;
</script>
<${tagName} {...classNameTransform(properties)}><Children {children} /></${tagName}>
`
)
);
tagNamesHaveNoChild.map((tagName) =>
writeFileSync(
join(dir, `${upperFirst(tagName)}.svelte`),
`<script lang="ts">
import { classNameTransform } from '../../utils';
export let properties: Record<string, unknown>;
</script>
<${tagName} {...classNameTransform(properties)} />
`
)
);
writeFileSync(
join(dir, 'index.ts'),
`${upperFirstTagNames
.map((tagName) => `import ${tagName} from './${tagName}.svelte';`)
.join('\n')}
const htmlComponents = {
${tagNamesHaveChildren.map((tagName) => `${tagName}: ${upperFirst(tagName)}`).join(',\n ')},
${tagNamesHaveNoChild.map((tagName) => `${tagName}: ${upperFirst(tagName)}`).join(',\n ')}
};
export {
${upperFirstTagNames.join(',\n ')}
};
export default htmlComponents;
`
);