From 067362427294d7317b79b6dafbb453982efaddcb Mon Sep 17 00:00:00 2001 From: Philip Nuzhnyi Date: Mon, 9 Sep 2024 12:05:25 +0100 Subject: [PATCH] improve svg handling --- CHANGELOG.md | 4 ++++ README.md | 4 ++++ package.json | 2 +- src/converter.ts | 7 +++++++ tests/converter.test.ts | 12 ++++++++++++ 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66c2e2b..a0833cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,3 +21,7 @@ All notable changes to the "html2ft" extension will be documented in this file. ## 0.5.0 - Relax VS code version requirements + +## 0.6.0 + +- Improve SVG handling diff --git a/README.md b/README.md index 79475c8..cb5177a 100644 --- a/README.md +++ b/README.md @@ -39,3 +39,7 @@ New converter ### 0.5.0 Relax VS code version requirements + +### 0.6.0 + +Improve SVG handling \ No newline at end of file diff --git a/package.json b/package.json index 280d0a8..82b6e44 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "html2ft", "displayName": "html2ft", "description": "Convert HTML to FastHTML FT", - "version": "0.5.0", + "version": "0.6.0", "publisher": "PhilipNuzhnyi", "repository": "https://github.com/callmephilip/html2ft", "license": "MIT", diff --git a/src/converter.ts b/src/converter.ts index d08d367..e0695d0 100644 --- a/src/converter.ts +++ b/src/converter.ts @@ -5,6 +5,8 @@ const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1); const listAttributes = (el: HTMLElement): string[] => Object.keys(el.attributes); +const wrapSvg = (svg: string): string => `NotStr("""${svg}""")`; + export function html2ft(html: string, attr1st: boolean = false): string { const revMap: { [key: string]: string } = { class: "cls", for: "fr" }; @@ -42,6 +44,11 @@ export function html2ft(html: string, attr1st: boolean = false): string { const tagName = el.tagName.toLowerCase().replace(/-/g, "_"); + // XX: a bit of a shortcut here for SVGs + if (tagName === "svg") { + return wrapSvg(el.toString()); + } + const cts = elm.childNodes; const cs = cts .map((c) => diff --git a/tests/converter.test.ts b/tests/converter.test.ts index 8bc64e4..a746c7b 100644 --- a/tests/converter.test.ts +++ b/tests/converter.test.ts @@ -8,6 +8,18 @@ const testCases = [ attr1st: false, expected: 'Div("hello")', }, + { + description: "svg", + input: + ` + + + + +`.replace(/\n/g, ""), + attr1st: false, + expected: `NotStr("""""")`, + }, { description: "somewhat complex stuff, children first", input: `