summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoraspizu <aspizu@protonmail.com>2025-03-10 04:12:38 +0530
committeraspizu <aspizu@protonmail.com>2025-03-10 04:12:38 +0530
commitb04c5eb7f783f8f83931fc8b479fd8aa45d7c00c (patch)
tree359cb03abc6ef394e0095c59448f7cc4c4369e10 /src
parent1656bbef4ac09f60d901c372617312888dd147d9 (diff)
feat: update README and package.json for version 0.3.0; add titleFormat option to GemtextConfig
Diffstat (limited to 'src')
-rw-r--r--src/index.ts5
-rw-r--r--src/vitePlugin.ts14
2 files changed, 18 insertions, 1 deletions
diff --git a/src/index.ts b/src/index.ts
index 2068d0f..beacb56 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -9,6 +9,11 @@ export interface GemtextConfig {
/** Absolute import path to the layout to use for all gemtext pages.
* If not provided, HMR and the Astro toolbar will not work. (default: none) */
layout?: string
+ /** The format to use for the page title. Can be one of:
+ * - `first-heading`: The first heading in the document will be used as the title. (default)
+ * - `filename`: The filename of the document will be used as the title.
+ */
+ titleFormat?: "first-heading" | "filename"
}
type SetupHookParams = HookParameters<"astro:config:setup"> & {
diff --git a/src/vitePlugin.ts b/src/vitePlugin.ts
index 853c3b8..138f8c8 100644
--- a/src/vitePlugin.ts
+++ b/src/vitePlugin.ts
@@ -5,6 +5,18 @@ import {ext, type GemtextConfig} from "./index.js"
function transform(code: string, id: string, config: GemtextConfig) {
const result = parse(code)
+ let title
+
+ if (config.titleFormat === "first-heading") {
+ for (const node of result.data) {
+ if (node._ === 4) {
+ title = node.text
+ break
+ }
+ }
+ } else if (config.titleFormat === "filename") {
+ title = pathToFileURL(id).pathname.split("/").pop()
+ }
const html = result.generate(HTMLRenderer)
return `
import {
@@ -28,7 +40,7 @@ export function getHeadings() {
}
export async function Content() {
const content = h(Fragment, {"set:html": html});
- ${config.layout && `return h(Layout, {title: url, children: content});`}
+ ${config.layout && `return h(Layout, {title: ${JSON.stringify(title)}}, content);`}
${!config.layout && `return content;`}
}
export default Content;