Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add htmlWithOptions #22

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .changeset/hot-eagles-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
"satori-html": minor
---

**NEW** Add `htmlWithOptions` function. Currently only supports control of `tailwind`.

```js
import { htmlWithOptions } from "satori-html";

const options = { tailwind: true };

// Tagged Template Literal
const tagged = htmlWithOptions(
options
)`<div class="color-${color}">hello, world</div>`;
// Function
const fn = htmlWithOptions(options)(
'<div class="color-red">hello, world</div>'
);
```
27 changes: 26 additions & 1 deletion packages/satori-html/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ Generate a [satori](https://github.com/vercel/satori)-friendly VDOM from a strin

Unfortunately, it is built on top of React's JSX and [expects "React-elements-like objects"](https://github.com/vercel/satori#use-without-jsx). This library (`satori-html`) bridges that gap, generating the necessary VDOM object from a string of HTML.

> **Note**
> [!NOTE]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to the new syntax.

> Satori supports a limited subset of HTML and CSS features, due to its special use case. Please use inline styles rather than class-based styling!

## Example

### API

#### `html`

`satori-html` exports an `html` helper, which transforms HTML strings into an object that is compatible with `satori`.

```js
Expand All @@ -38,3 +40,26 @@ const tagged = html`<div style="color: ${color};">hello, world</div>`;
// Function
const fn = html('<div style="color: black;">hello, world</div>');
```

#### `htmlWithOptions`

For more advanced customization `htmlWithOptions` is exposed.

```js
import { htmlWithOptions } from "satori-html";

// Tagged Template Literal
const tagged = htmlWithOptions(
options
)`<div class="color-${color}">hello, world</div>`;
// Function
const fn = htmlWithOptions(options)(
'<div class="color-red">hello, world</div>'
);
```

##### Available Options

| Option | Type | Default | `html` Configuration |
| ---------- | --------- | ------- | -------------------- |
| `tailwind` | `boolean` | `false` | `true` |
16 changes: 8 additions & 8 deletions packages/satori-html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
},
"license": "MIT",
"dependencies": {
"ultrahtml": "^1.2.0"
"ultrahtml": "^1.5.2"
},
"devDependencies": {
"@resvg/resvg-js": "^2.1.0",
"@types/node": "^18.8.4",
"jest-image-snapshot": "^5.2.0",
"satori": "^0.0.38",
"typescript": "^4.8.4",
"vite": "^3.1.7",
"vitest": "^0.24.1"
"@resvg/resvg-js": "^2.6.0",
"@types/node": "^20.11.0",
"jest-image-snapshot": "^6.4.0",
"satori": "^0.10.11",
"typescript": "^5.3.3",
"vite": "^5.0.11",
"vitest": "^1.2.0"
}
}
109 changes: 60 additions & 49 deletions packages/satori-html/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,62 +77,73 @@ interface VNode {
};
}

export function html(
templates: string | TemplateStringsArray,
...expressions: any[]
): VNode {
const result = __html.call(null, templates, ...expressions);
let doc = parse(result.value.trim());
inliner(doc);
tw(doc);
export interface SatoriHtmlOptions {
tailwind?: boolean;
}

export function htmlWithOption(options?: SatoriHtmlOptions) {
return function html(
templates: string | TemplateStringsArray,
...expressions: any[]
): VNode {
const result = __html.call(null, templates, ...expressions);
let doc = parse(result.value.trim());
inliner(doc);

if (options?.tailwind) {
tw(doc);
}
Comment on lines +93 to +95
Copy link
Author

@KiwiKilian KiwiKilian Jan 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've only changed this part, otherwise it's just returning the same function as before.


const nodeMap = new WeakMap();
let root: VNode = {
type: "div",
props: {
style: {
display: "flex",
flexDirection: "column",
width: "100%",
height: "100%",
const nodeMap = new WeakMap();
let root: VNode = {
type: "div",
props: {
style: {
display: "flex",
flexDirection: "column",
width: "100%",
height: "100%",
},
children: [],
},
children: [],
},
};
walkSync(doc, (node, parent, index) => {
let newNode: any = {};
if (node.type === DOCUMENT_NODE) {
nodeMap.set(node, root);
} else if (node.type === ELEMENT_NODE) {
newNode.type = node.name;
const { style, "": _, ...props } = node.attributes as any;
if (typeof style === "object") {
props["style"] = {};
for (const [decl, value] of Object.entries(style)) {
props["style"][camelize(decl)] = value;
};
walkSync(doc, (node, parent, index) => {
let newNode: any = {};
if (node.type === DOCUMENT_NODE) {
nodeMap.set(node, root);
} else if (node.type === ELEMENT_NODE) {
newNode.type = node.name;
const { style, "": _, ...props } = node.attributes as any;
if (typeof style === "object") {
props["style"] = {};
for (const [decl, value] of Object.entries(style)) {
props["style"][camelize(decl)] = value;
}
}
}
props.children = [] as unknown as string;
Object.assign(newNode, { props });
nodeMap.set(node, newNode);
if (parent) {
const newParent = nodeMap.get(parent);
newParent.props.children[index] = newNode;
}
} else if (node.type === TEXT_NODE) {
newNode = node.value.trim();
if (newNode.trim()) {
props.children = [] as unknown as string;
Object.assign(newNode, { props });
nodeMap.set(node, newNode);
if (parent) {
const newParent = nodeMap.get(parent);
if (parent.children.length === 1) {
newParent.props.children = newNode;
} else {
newParent.props.children[index] = newNode;
newParent.props.children[index] = newNode;
}
} else if (node.type === TEXT_NODE) {
newNode = node.value.trim();
if (newNode.trim()) {
if (parent) {
const newParent = nodeMap.get(parent);
if (parent.children.length === 1) {
newParent.props.children = newNode;
} else {
newParent.props.children[index] = newNode;
}
}
}
}
}
});
});

return root;
return root;
};
}

export const html = htmlWithOption({ tailwind: true });
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 11 additions & 1 deletion packages/satori-html/test/tw.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from "vitest";
import { html } from "../src/index";
import { html, htmlWithOption } from "../src/index";

describe("tailwind", () => {
it("picks up margin util", async () => {
Expand Down Expand Up @@ -118,4 +118,14 @@ describe("tailwind", () => {
const result = html`<div class="z-0">Hello world</div>`;
expect("tw" in result.props.children?.[0].props).toEqual(true);
});
it("can be disabled", async () => {
const result = htmlWithOption({
tailwind: false,
})`<div class="flex">Hello world</div>`;
expect("tw" in result.props.children?.[0].props).toEqual(false);
});
it("is disabled per default", async () => {
const result = htmlWithOption()`<div class="flex">Hello world</div>`;
expect("tw" in result.props.children?.[0].props).toEqual(false);
});
});
Loading