Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/maddsua/jsxml
Browse files Browse the repository at this point in the history
  • Loading branch information
maddsua committed Jan 8, 2024
2 parents 2c70681 + 31a5ce2 commit 6a5f272
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
34 changes: 34 additions & 0 deletions lib/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,37 @@ const reactNamingConventions = new Map<string, string>([
["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 {
Expand Down Expand Up @@ -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 <br> tag replacement
*/
Expand Down
2 changes: 1 addition & 1 deletion test/templates/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const pets = [
export default () => {
return <Layout title="Test title">
<p class={{ 'added-class': true, 'hidden-class': false }}>
Nexted paragraph here
Nested paragraph here
</p>
<ul class={['class-1', 'class-2']}>
{ pets.map(item => <li>{item}</li>) }
Expand Down

0 comments on commit 6a5f272

Please sign in to comment.