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 ability to customize class on main element #173 #227

Merged
merged 1 commit into from
Jan 16, 2024
Merged
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
39 changes: 36 additions & 3 deletions packages/amagaki-plugin-page-builder/src/page-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,17 @@ export interface PageBuilderOptions {
/** Append extra HTML to the bottom of the <head> element. */
extra?: string[];
};
main ?: {
/**
* Add a class on the <main> element. The class can either be a
* string or an async function that returns a string.
*/
class?: string | ((context: TemplateContext) => Promise<string>);
/** Prepend HTML to the top of the <main> element. */
prepend?: string[];
/** Append extra HTML to the bottom of the <main> element. */
extra?: string[];
},
partialPaths?: PartialPaths;
/** Options for generating the sitemap. */
sitemapXml?: {
Expand Down Expand Up @@ -335,7 +346,7 @@ export class PageBuilder {
return this.doc.fields[name] ?? this.doc.collection?.fields[name];
}

async buildBodyTag() {
async buildBodyElement() {
if (this.options.body?.class) {
const className =
typeof this.options.body?.class === 'function'
Expand All @@ -347,6 +358,18 @@ export class PageBuilder {
}
}

async buildMainElement() {
if (this.options.main?.class) {
const className =
typeof this.options.main?.class === 'function'
? await this.options.main?.class(this.context)
: this.options.main?.class;
return html`<main class="${className}">`;
} else {
return html`<main>`;
}
}

async buildDocument() {
const partials =
this.doc.fields.partials ?? this.doc.collection?.fields.partials;
Expand All @@ -356,7 +379,7 @@ export class PageBuilder {
this.doc.locale
)}" itemscope itemtype="https://schema.org/WebPage">
${await this.buildHeadElement()}
${await this.buildBodyTag()}
${await this.buildBodyElement()}
${
this.options.body?.prepend
? safeString(await this.buildExtraElements(this.options.body?.prepend))
Expand All @@ -368,7 +391,12 @@ export class PageBuilder {
? ''
: await this.buildBuiltinPartial('header', this.options.header)
}
<main>
${await this.buildMainElement()}
${
this.options.main?.prepend
? safeString(await this.buildExtraElements(this.options.main?.prepend))
: ''
}
${safeString(
(
await Promise.all(
Expand All @@ -378,6 +406,11 @@ export class PageBuilder {
)
).join('\n')
)}
${
this.options.main?.extra
? safeString(await this.buildExtraElements(this.options.main?.extra))
: ''
}
</main>
${
this.getFieldValue('footer') === false || this.options.footer?.enabled === false
Expand Down
Loading