From 31a5ce2f6d212d657a6862be7b6763b1bfb4792b Mon Sep 17 00:00:00 2001 From: Daniel <102395937+maddsua@users.noreply.github.com> Date: Mon, 8 Jan 2024 05:37:24 +0000 Subject: [PATCH] Feat limited tag sets (#13) * Update jsx.ts * Update Component.tsx --- lib/jsx.ts | 34 ++++++++++++++++++++++++++++++++++ test/templates/Component.tsx | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/jsx.ts b/lib/jsx.ts index ec34f37..149ae00 100644 --- a/lib/jsx.ts +++ b/lib/jsx.ts @@ -24,12 +24,37 @@ const reactNamingConventions = new Map([ ["className", "class"] ]); +const htmlTagSets = { + telegram: { + tags: new Set([ + "b", + "bold", + "i", + "em", + "u", + "ins", + "s", + "strike", + "del", + "span", + "tg-spoiler", + "a", + "tg-emoji", + "code", + "pre", + "blockquote", + "br" // this one requires "convertBrTagsToNewlines" to be set to "true" + ]) + } +}; + const collapseWhitespaces = (html: string) => html.replace(/[\t ]+/g, ' ').replace(/[\r\n]/g, ''); interface RenderProps { addDoctype?: boolean; convertBrTagsToNewlines?: boolean; minifyHTML?: boolean; + restrictTagSet?: keyof typeof htmlTagSets; }; abstract class JSXNode { @@ -136,6 +161,15 @@ class JSXHTMLNode extends JSXNode { render(renderProps?: RenderProps) { + /** + * Check tag set restrictions + */ + if (renderProps?.restrictTagSet) { + const useTagSet = htmlTagSets[renderProps.restrictTagSet]; + if (useTagSet && !useTagSet.tags.has(this.tagname)) + throw new Error(`Tagset "${renderProps.restrictTagSet}" disallows usage of the <${this.tagname}> tag`); + } + /** * Check for
tag replacement */ diff --git a/test/templates/Component.tsx b/test/templates/Component.tsx index 5f7e1e9..dd04c45 100644 --- a/test/templates/Component.tsx +++ b/test/templates/Component.tsx @@ -11,7 +11,7 @@ const pets = [ export default () => { return

- Nexted paragraph here + Nested paragraph here