Skip to content

Commit

Permalink
0.2.0: Support <script> elements
Browse files Browse the repository at this point in the history
  • Loading branch information
Tao-VanJS committed Sep 25, 2023
1 parent 8b61922 commit 93fa0cf
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"divs"
]
}
26 changes: 26 additions & 0 deletions converter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,32 @@ test("htmlToVanCode: nested spans", t => t.deepEqual(
},
))

test("htmlToVanCode: script", t => t.deepEqual(
htmlToVanCode('<script type="text/javascript" src="/prism.js"></script>'),
{
code: [
'script({type: "text/javascript", src: "/prism.js"})',
],
tags: ["script"],
components: [],
},
))

test("htmlToVanCode: inline script", t => t.deepEqual(
htmlToVanCode(`<script>
console.log("Hello, World!")
</script>`),
{
code: [
'script(',
' "\\n console.log(\\"Hello, World!\\")\\n",',
')',
],
tags: ["script"],
components: [],
},
))

test("mdToVanCode: Hello", t => t.deepEqual(
mdToVanCode(`👋Hello
* 🗺️World
Expand Down
3 changes: 2 additions & 1 deletion converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ type Dom = Element | Text
const filterDoms = (doms: readonly ChildNode[], skipEmptyText: boolean) =>
<Dom[]>doms.filter(
c => c.type === "tag" && c.name !== dummy ||
c.type === "text" && (!skipEmptyText || /\S/.test(c.data)))
c.type === "text" && (!skipEmptyText || /\S/.test(c.data)) ||
c.type === "script")

export interface HtmlToVanCodeOptions {
indent?: number
Expand Down
3 changes: 2 additions & 1 deletion dist/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { marked } from 'marked';
const dummy = "DUMMY";
const quoteIfNeeded = (key) => /^[a-zA-Z_][a-zA-Z_0-9]+$/.test(key) ? key : `"${key}"`;
const filterDoms = (doms, skipEmptyText) => doms.filter(c => c.type === "tag" && c.name !== dummy ||
c.type === "text" && (!skipEmptyText || /\S/.test(c.data)));
c.type === "text" && (!skipEmptyText || /\S/.test(c.data)) ||
c.type === "script");
export const htmlToVanCode = (html, { indent = 2, spacing = false, skipEmptyText = false, htmlTagPred = s => s.toLowerCase() === s, } = {}) => {
const attrsToVanCode = (attrs, children) => {
const space = spacing ? " " : "";
Expand Down
18 changes: 18 additions & 0 deletions dist/converter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,24 @@ test("htmlToVanCode: nested spans", t => t.deepEqual(htmlToVanCode('<span><span>
tags: ["span"],
components: [],
}));
test("htmlToVanCode: script", t => t.deepEqual(htmlToVanCode('<script type="text/javascript" src="/prism.js"></script>'), {
code: [
'script({type: "text/javascript", src: "/prism.js"})',
],
tags: ["script"],
components: [],
}));
test("htmlToVanCode: inline script", t => t.deepEqual(htmlToVanCode(`<script>
console.log("Hello, World!")
</script>`), {
code: [
'script(',
' "\\n console.log(\\"Hello, World!\\")\\n",',
')',
],
tags: ["script"],
components: [],
}));
test("mdToVanCode: Hello", t => t.deepEqual(mdToVanCode(`👋Hello
* 🗺️World
* [🍦VanJS](https://vanjs.org/)
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vanjs-converter",
"version": "0.1.3",
"version": "0.2.0",
"description": "Utility to convert MD or HTML text into VanJS code",
"type": "module",
"files": [
Expand All @@ -10,7 +10,9 @@
"main": "dist/converter.js",
"browser": "dist/converter.js",
"scripts": {
"build": "bash build.sh"
"build": "tsc",
"watch": "tsc --watch",
"test": "ava"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 93fa0cf

Please sign in to comment.