Skip to content

Commit 5539187

Browse files
committed
feat: JSON strings in data attributes, close #46
1 parent 3ab5e82 commit 5539187

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,14 @@ function render(tree?: Node | Node[], options: Options = {}): string {
149149

150150
for (const [key, value] of Object.entries(object)) {
151151
if (typeof value === 'string') {
152-
if (quoteAllAttributes || ATTRIBUTE_QUOTES_REQUIRED.test(value)) {
152+
let json;
153+
try {
154+
json = JSON.parse(value);
155+
} catch {}
156+
157+
if (json) {
158+
attr += ` ${key}='${value}'`;
159+
} else if (quoteAllAttributes || ATTRIBUTE_QUOTES_REQUIRED.test(value)) {
153160
let attrValue = value;
154161

155162
if (replaceQuote) {

0 commit comments

Comments
 (0)