| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
p. |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
s) |
| 11 |
X) |
| 12 |
AST) |
| 13 |
s) |
| 14 |
TML) |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
import { Feed } from 'feed'; |
| 21 |
import rehypeStringify from 'rehype-stringify'; |
| 22 |
import remarkMdx from 'remark-mdx'; |
| 23 |
import remarkParse from 'remark-parse'; |
| 24 |
import remarkRehype from 'remark-rehype'; |
| 25 |
import { unified } from 'unified'; |
| 26 |
|
| 27 |
import { fileAsMarkdown, getFolder } from './content'; |
| 28 |
|
| 29 |
*/ |
| 30 |
const FEED_COLLECTIONS = ['essays', 'interviews']; |
| 31 |
|
| 32 |
const FEED_TITLE = '</> htmx - high power tools for html'; |
| 33 |
const FEED_DESCRIPTION = 'Essays and interviews from the htmx project.'; |
| 34 |
const FEED_LANGUAGE = 'en'; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
function remarkStripMdxNodes() { |
| 42 |
|
| 43 |
return (tree) => { |
| 44 |
if (!Array.isArray(tree.children)) return; |
| 45 |
const drop = new Set(['mdxjsEsm', 'mdxJsxFlowElement', 'mdxJsxTextElement', 'mdxFlowExpression', 'mdxTextExpression']); |
| 46 |
|
| 47 |
const walk = (node) => { |
| 48 |
if (Array.isArray(node.children)) { |
| 49 |
node.children = node.children.filter( (c) => !drop.has(c.type)); |
| 50 |
node.children.forEach(walk); |
| 51 |
} |
| 52 |
}; |
| 53 |
walk(tree); |
| 54 |
}; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
function rehypeAbsoluteUrls(origin) { |
| 65 |
|
| 66 |
|
| 67 |
const rawRe = /\b(href|src)=("|')(\/[^/][^"']*?)\2/g; |
| 68 |
|
| 69 |
return (tree) => { |
| 70 |
|
| 71 |
const walk = (node) => { |
| 72 |
if (node.type === 'element' && node.properties) { |
| 73 |
for (const attr of ['href', 'src']) { |
| 74 |
const v = node.properties[attr]; |
| 75 |
if (typeof v === 'string' && v.startsWith('/') && !v.startsWith('//')) { |
| 76 |
node.properties[attr] = origin + v; |
| 77 |
} |
| 78 |
} |
| 79 |
} else if (node.type === 'raw' && typeof node.value === 'string') { |
| 80 |
node.value = node.value.replace(rawRe, (_m, attr, q, path) => `${attr}=${q}${origin}${path}${q}`); |
| 81 |
} |
| 82 |
if (Array.isArray(node.children)) node.children.forEach(walk); |
| 83 |
}; |
| 84 |
walk(tree); |
| 85 |
}; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
async function markdownToHtml(markdown, origin, isMdx) { |
| 99 |
let processor = unified().use(remarkParse); |
| 100 |
if (isMdx) processor = processor.use(remarkMdx).use(remarkStripMdxNodes); |
| 101 |
processor = processor |
| 102 |
.use(remarkRehype, { allowDangerousHtml: true }) |
| 103 |
.use(() => rehypeAbsoluteUrls(origin)) |
| 104 |
.use(rehypeStringify, { allowDangerousHtml: true }); |
| 105 |
const result = await processor.process(markdown); |
| 106 |
return String(result); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
function stripTitlePrefix(md) { |
| 111 |
return md.replace(/^# [^\n]*\n+/, ''); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
export async function buildFeed(site) { |
| 122 |
const origin = (site?.href ?? 'https://htmx.org/').replace(/\/$/, ''); |
| 123 |
|
| 124 |
const feed = new Feed({ |
| 125 |
title: FEED_TITLE, |
| 126 |
description: FEED_DESCRIPTION, |
| 127 |
id: `${origin}/`, |
| 128 |
link: `${origin}/`, |
| 129 |
language: FEED_LANGUAGE, |
| 130 |
feedLinks: { |
| 131 |
atom: `${origin}/atom.xml`, |
| 132 |
rss: `${origin}/rss.xml`, |
| 133 |
}, |
| 134 |
copyright: '', |
| 135 |
}); |
| 136 |
|
| 137 |
|
| 138 |
const entries = []; |
| 139 |
for (const collection of FEED_COLLECTIONS) { |
| 140 |
const folder = await getFolder(collection); |
| 141 |
for (const file of folder.allFiles) { |
| 142 |
const fm = file.frontmatter; |
| 143 |
if (!fm.created) continue; |
| 144 |
entries.push({ |
| 145 |
file, |
| 146 |
published: new Date(fm.created), |
| 147 |
updated: fm.modified ? new Date(fm.modified) : new Date(fm.created), |
| 148 |
}); |
| 149 |
} |
| 150 |
} |
| 151 |
entries.sort((a, b) => b.published.getTime() - a.published.getTime()); |
| 152 |
|
| 153 |
for (const { file, published, updated } of entries) { |
| 154 |
const body = stripTitlePrefix(fileAsMarkdown(file.path)); |
| 155 |
const isMdx = file.path.endsWith('.mdx'); |
| 156 |
const content = await markdownToHtml(body, origin, isMdx); |
| 157 |
const link = `${origin}${file.url}`; |
| 158 |
|
| 159 |
feed.addItem({ |
| 160 |
title: file.frontmatter.title, |
| 161 |
id: link, |
| 162 |
link, |
| 163 |
description: file.frontmatter.description ?? '', |
| 164 |
content, |
| 165 |
date: updated, |
| 166 |
published, |
| 167 |
author: (file.frontmatter.authors ?? []).map( (name) => ({ name })), |
| 168 |
category: (file.frontmatter.tags ?? []).map( (name) => ({ name })), |
| 169 |
}); |
| 170 |
} |
| 171 |
|
| 172 |
return feed; |
| 173 |
} |