|
| 1 | +import { merge } from "lume/core/utils.ts"; |
| 2 | +import { Page } from "lume/core/filesystem.ts"; |
| 3 | +import { buildSort } from "lume/plugins/search.ts"; |
| 4 | + |
| 5 | +import { formatXML } from "./deps.ts"; |
| 6 | +import type { FormatOptions } from "./deps.ts"; |
| 7 | + |
| 8 | +import type { Site } from "lume/core.ts"; |
| 9 | +import type { Search } from "lume/plugins/search.ts"; |
| 10 | +import type { MetaData } from "lume/plugins/metas.ts"; |
| 11 | + |
| 12 | +export interface Options { |
| 13 | + /** The query to search pages included in the feed. defaults to `type=post` */ |
| 14 | + query: string[]; |
| 15 | + |
| 16 | + /** The values to sort the feeds pages. defaults to `date=desc` */ |
| 17 | + sort: string[]; |
| 18 | + |
| 19 | + /** The limit to display pages. defaults to `10` */ |
| 20 | + limit: number; |
| 21 | + |
| 22 | + /** Options passed to xml-formatter */ |
| 23 | + options: Partial<FormatOptions>; |
| 24 | +} |
| 25 | + |
| 26 | +// Default options |
| 27 | +export const defaults: Options = { |
| 28 | + query: ["type=post"], |
| 29 | + sort: ["date=desc"], |
| 30 | + limit: 10, |
| 31 | + options: { |
| 32 | + indentation: " ", |
| 33 | + collapseContent: true, |
| 34 | + lineSeparator: "\n", |
| 35 | + }, |
| 36 | +}; |
| 37 | + |
| 38 | +export interface FeedMetaData extends MetaData { |
| 39 | + author: { |
| 40 | + name: string; |
| 41 | + email: string; |
| 42 | + url: string; |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +/** A plugin to <description> */ |
| 47 | +export default function (userOptions?: Partial<Options>) { |
| 48 | + const options = merge(defaults, userOptions); |
| 49 | + |
| 50 | + return (site: Site) => { |
| 51 | + site.addEventListener("afterRender", () => { |
| 52 | + // Create the sitemap.xml page |
| 53 | + const feed = Page.create("feed.xml", getFeedContent(site)); |
| 54 | + |
| 55 | + // Add to the sitemap page to pages |
| 56 | + site.pages.push(feed); |
| 57 | + }); |
| 58 | + |
| 59 | + function getFeedContent(site: Site) { |
| 60 | + // Get the pages |
| 61 | + const search = site.globalData.search as Search; |
| 62 | + const feedPages = search.pages( |
| 63 | + options.query, |
| 64 | + options.sort, |
| 65 | + options.limit, |
| 66 | + ); |
| 67 | + |
| 68 | + // Sort the pages |
| 69 | + feedPages.sort(buildSort(options.sort)); |
| 70 | + |
| 71 | + const metas = isActiveProcessor(site, "metas") |
| 72 | + ? feedPages[0].data.metas as FeedMetaData |
| 73 | + : feedPages[0].data.site as FeedMetaData; |
| 74 | + |
| 75 | + // deno-fmt-ignore |
| 76 | + const atomfeed = `<?xml version="1.0" encoding="utf-8"?> |
| 77 | +<feed xmlns="http://www.w3.org/2005/Atom"> |
| 78 | + <title>${metas.title}</title> |
| 79 | + <subtitle>${metas.description}</subtitle> |
| 80 | + <link href="${site.url("feed.xml", true)}" rel="self"/> |
| 81 | + <link href="${site.url("/", true)}"/> |
| 82 | + <updated>${feedPages[0].data.date?.toISOString()}</updated> |
| 83 | + <id>${site.url("/", true)}</id> |
| 84 | + <author> |
| 85 | + <name>${metas.author.name}</name> |
| 86 | + </author> |
| 87 | +
|
| 88 | + ${feedPages.map((post: Page) => { |
| 89 | + return `<entry> |
| 90 | + <title>${post.data.title}</title> |
| 91 | + <link href="${site.url(post.data.url as string, true)}"/> |
| 92 | + <id>${site.url(post.data.url as string, true)}</id> |
| 93 | + <updated>${post.data.date?.toISOString()}</updated> |
| 94 | + <summary>${post.data.excerpt}</summary> |
| 95 | + </entry> |
| 96 | + `}).join("").trim()} |
| 97 | +</feed>`.trim(); |
| 98 | + |
| 99 | + return formatXML(atomfeed, options.options); |
| 100 | + } |
| 101 | + |
| 102 | + function isActiveProcessor(site: Site, processor: string): boolean { |
| 103 | + const { processors } = site.processors; |
| 104 | + |
| 105 | + // deno-lint-ignore no-unused-vars |
| 106 | + for (const [value, key] of processors) { |
| 107 | + if (typeof value === "function" && value.name === processor) { |
| 108 | + return true; |
| 109 | + } |
| 110 | + } |
| 111 | + return false; |
| 112 | + } |
| 113 | + }; |
| 114 | +} |
0 commit comments