Skip to content

Commit

Permalink
clean up attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
callmephilip committed Sep 7, 2024
1 parent 86d7b7b commit 599612d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ All notable changes to the "html2ft" extension will be documented in this file.

## 0.3.0

Support extension settings
Support extension settings

## 0.4.0

New converter
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ Run conversion locally
### 0.3.0

Support extension settings

### 0.4.0

New converter
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "html2ft",
"displayName": "html2ft",
"description": "Convert HTML to FastHTML FT",
"version": "0.4.0",
"version": "0.4.1",
"publisher": "PhilipNuzhnyi",
"repository": "https://github.com/callmephilip/html2ft",
"license": "MIT",
Expand Down
8 changes: 6 additions & 2 deletions src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,24 @@ export function html2ft(html: string, attr1st: boolean = false): string {

for (const key of elmAttrs) {
const value = el.getAttribute(key);

if (typeof value === "undefined") {
continue;
}

const mappedKey = revMap[key] || key;
const formattedKey = mappedKey.replace(/-/g, "_");

// clean up value by removing extra spaces and newlines
const cleanedValue = value.trim().replace(/\s+/g, " ");

if (/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(formattedKey)) {
attrs.push(
`${formattedKey}=${JSON.stringify(value).replaceAll('"', "'")}`
`${formattedKey}=${JSON.stringify(cleanedValue).replaceAll('"', "'")}`
);
} else {
attrs.push(
`**{${JSON.stringify(key)}: ${JSON.stringify(value).replaceAll(
`**{${JSON.stringify(key)}: ${JSON.stringify(cleanedValue).replaceAll(
'"',
"'"
)}}`
Expand Down
37 changes: 37 additions & 0 deletions tests/converter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,43 @@ const testCases = [
Body(
H1("This is a Heading"),
P("This is a paragraph.")
)`,
},
{
description: "multiline class attribute",
input: `<div class="fixed top-0 left-0 h-full w-64
bg-white shadow-lg overflow-y-auto
transform -translate-x-full
transition-transform duration-300
ease-in-out z-50">
<div class="p-6">
<div class="text-center mb-4">
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240429130139/employee.png"
alt="Profile Picture"
class="rounded-full
mx-auto mb-2">
<h3 class="text-lg font-semibold">
Sahil Trivedi
</h3>
<p class="text-gray-600">
Web Developer
</p>
</div>
</div>
</div>`,
attr1st: false,
expected: `Div(
Div(
Div(
Img(src='https://media.geeksforgeeks.org/wp-content/uploads/20240429130139/employee.png', alt='Profile Picture', cls='rounded-full mx-auto mb-2'),
H3("Sahil Trivedi", cls='text-lg font-semibold'),
P("Web Developer", cls='text-gray-600'),
cls='text-center mb-4'
),
cls='p-6'
),
cls='fixed top-0 left-0 h-full w-64 bg-white shadow-lg overflow-y-auto transform -translate-x-full transition-transform duration-300 ease-in-out z-50'
)`,
},
];
Expand Down

0 comments on commit 599612d

Please sign in to comment.