Skip to content

Commit

Permalink
feat: injector - main method and single TagDescriptor input
Browse files Browse the repository at this point in the history
  • Loading branch information
rossrobino committed Jan 9, 2025
1 parent 166cf5b commit 8428e9a
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 49 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-plums-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"domco": minor
---

feat: [Injector] adds `main` method to inject into the `main` element. A single `TagDescriptor` can now be provided instead of an array into injection methods.
42 changes: 36 additions & 6 deletions apps/docs/src/server/content/generated/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ The HTML string.

> **body**(`tags`, `method`): [`Injector`](globals.md#injector)
Defined in: [injector/index.ts:196](https://github.com/rossrobino/domco/blob/main/packages/domco/src/injector/index.ts#L196)
Defined in: [injector/index.ts:200](https://github.com/rossrobino/domco/blob/main/packages/domco/src/injector/index.ts#L200)

Inject tags into the `body` element.

Expand Down Expand Up @@ -113,7 +113,7 @@ The Injector instance.

> **comment**(`text`, `tags`): [`Injector`](globals.md#injector)
Defined in: [injector/index.ts:140](https://github.com/rossrobino/domco/blob/main/packages/domco/src/injector/index.ts#L140)
Defined in: [injector/index.ts:144](https://github.com/rossrobino/domco/blob/main/packages/domco/src/injector/index.ts#L144)

Replace comments with tags.

Expand Down Expand Up @@ -143,7 +143,7 @@ The Injector instance.

> **head**(`tags`, `method`): [`Injector`](globals.md#injector)
Defined in: [injector/index.ts:170](https://github.com/rossrobino/domco/blob/main/packages/domco/src/injector/index.ts#L170)
Defined in: [injector/index.ts:174](https://github.com/rossrobino/domco/blob/main/packages/domco/src/injector/index.ts#L174)

Inject tags into the `head` element.

Expand All @@ -167,13 +167,43 @@ Add tags at the end, beginning, or replace. - defaults to `"append"`

The Injector instance.

<a id="main"></a>

##### main()

> **main**(`tags`, `method`): [`Injector`](globals.md#injector)
Defined in: [injector/index.ts:221](https://github.com/rossrobino/domco/blob/main/packages/domco/src/injector/index.ts#L221)

Inject tags into the `main` element, appends `main` + tags to `body` if not found.

###### Parameters

###### tags

[`TagInput`](globals.md#taginput)

Tags to inject.

###### method

[`InjectMethod`](globals.md#injectmethod) = `"append"`

Add tags at the end, beginning, or replace. - defaults to `"append"`

###### Returns

[`Injector`](globals.md#injector)

The Injector instance.

<a id="title"></a>

##### title()

> **title**(`text`): [`Injector`](globals.md#injector)
Defined in: [injector/index.ts:155](https://github.com/rossrobino/domco/blob/main/packages/domco/src/injector/index.ts#L155)
Defined in: [injector/index.ts:159](https://github.com/rossrobino/domco/blob/main/packages/domco/src/injector/index.ts#L159)

Set or change the document's title element.

Expand Down Expand Up @@ -618,11 +648,11 @@ The tagName of the element.

### TagInput

> **TagInput**: `string` \| [`TagDescriptor`](globals.md#tagdescriptor)[]
> **TagInput**: `string` \| [`TagDescriptor`](globals.md#tagdescriptor) \| [`TagDescriptor`](globals.md#tagdescriptor)[]
Defined in: [types/index.ts:172](https://github.com/rossrobino/domco/blob/main/packages/domco/src/types/index.ts#L172)

Tags can be a string, or an array of TagDescriptors.
Tags can be a `string`, a `TagDescriptor`, or an array of `TagDescriptors`.

## Functions

Expand Down
2 changes: 1 addition & 1 deletion apps/tester/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"preview": "vite preview"
},
"devDependencies": {
"@types/react": "^19.0.2",
"@types/react": "^19.0.4",
"@types/react-dom": "^19.0.2",
"@vitejs/plugin-react": "^4.3.4",
"domco": "*",
Expand Down
54 changes: 27 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
"@robino/md": "^1.0.1",
"@robino/prettier": "^0.1.1",
"@robino/tsconfig": "^0.2.2",
"hono": "^4.6.15",
"hono": "^4.6.16",
"prettier": "^3.4.2",
"prettier-plugin-tailwindcss": "^0.6.9",
"turbo": "^2.3.3",
"typescript": "^5.7.2",
"vite": "^6.0.6"
"typescript": "^5.7.3",
"vite": "^6.0.7"
}
}
4 changes: 2 additions & 2 deletions packages/create-domco/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
},
"license": "MIT",
"dependencies": {
"@clack/prompts": "^0.9.0",
"domco": "^2.2.0",
"@clack/prompts": "^0.9.1",
"domco": "^2.2.2",
"prettier": "^3.4.2",
"which-pm-runs": "^1.1.0"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/create-domco/src/dependencies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const getDependencies = async () => {
prettier: "3.4.2",
prettierTailwind: "0.6.9",
tailwind: "3.4.17",
typescript: "5.7.2",
vite: "6.0.6",
typescript: "5.7.3",
vite: "6.0.7",
};
};
30 changes: 24 additions & 6 deletions packages/domco/src/injector/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,13 @@ export class Injector {
static serializeTags(tags: TagDescriptor["children"]): string {
if (tags instanceof Array) {
return tags.map((tag) => this.#serializeTag(tag)).join("");
} else if (typeof tags === "string") {
return tags;
} else if (tags) {
return this.#serializeTag(tags);
}

return tags ?? "";
return "";
}

/**
Expand Down Expand Up @@ -156,7 +160,7 @@ export class Injector {
try {
return this.#inject("title", text, "replace");
} catch {
return this.head([{ name: "title", children: text }]);
return this.head({ name: "title", children: text });
}
}

Expand All @@ -176,7 +180,7 @@ export class Injector {
// try to prepend a new head element to html
return this.#inject(
"html",
[{ name: "head", children: tags }],
{ name: "head", children: tags },
"prepend",
);
} catch {
Expand All @@ -195,16 +199,30 @@ export class Injector {
*/
body(tags: TagInput, method: InjectMethod = "append") {
try {
// try to inject into body
return this.#inject("body", tags, method);
} catch {
try {
// try to append a new body element to html
return this.#inject("html", [{ name: "body", children: tags }]);
// append a new body element to html
return this.#inject("html", { name: "body", children: tags });
} catch {
this.#html += Injector.serializeTags(tags);
return this;
}
}
}

/**
* Inject tags into the `main` element, appends `main` + tags to `body` if not found.
*
* @param tags Tags to inject.
* @param method Add tags at the end, beginning, or replace. - defaults to `"append"`
* @returns The Injector instance.
*/
main(tags: TagInput, method: InjectMethod = "append") {
try {
return this.#inject("main", tags, method);
} catch {
return this.body({ name: "main", children: tags }, "append");
}
}
}
4 changes: 2 additions & 2 deletions packages/domco/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ export type TagDescriptor = {
children?: TagInput;
};

/** Tags can be a string, or an array of TagDescriptors. */
export type TagInput = string | TagDescriptor[];
/** Tags can be a `string`, a `TagDescriptor`, or an array of `TagDescriptors`. */
export type TagInput = string | TagDescriptor | TagDescriptor[];

/** How to inject tags into the HTML string. */
export type InjectMethod = "append" | "prepend" | "replace";

0 comments on commit 8428e9a

Please sign in to comment.