@binarynoir/vite-plugin-blog 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -23
- package/dist/index.cjs +146 -24
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +36 -1
- package/dist/index.d.ts +36 -1
- package/dist/index.js +146 -24
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -767,15 +767,25 @@ function createLoader(options, assetUrl) {
|
|
|
767
767
|
posts.push(post);
|
|
768
768
|
}
|
|
769
769
|
const published = new Set(posts.map((post) => post.slug));
|
|
770
|
+
const usedTags2 = new Set(posts.flatMap((post) => post.tags));
|
|
770
771
|
for (const [post, links2] of linksByPost) {
|
|
771
772
|
for (const { href, line } of links2) {
|
|
772
773
|
const where = line ? `line ${line}: ` : "";
|
|
773
774
|
const pathname = href.split(/[?#]/)[0];
|
|
774
775
|
if (pathname.startsWith(options.base)) {
|
|
775
776
|
const rest = pathname.slice(options.base.length).replace(/(?:\/index\.html|\/)$/, "");
|
|
776
|
-
if (!rest
|
|
777
|
-
|
|
778
|
-
|
|
777
|
+
if (!rest) continue;
|
|
778
|
+
const tag = /^tag\/([^/]+)$/.exec(rest)?.[1];
|
|
779
|
+
if (tag) {
|
|
780
|
+
if (options.tagPages !== false && usedTags2.has(tag)) continue;
|
|
781
|
+
issues.push({
|
|
782
|
+
file: post.file,
|
|
783
|
+
message: `${where}link to ${href} goes nowhere: no published post has the tag "${tag}"`
|
|
784
|
+
});
|
|
785
|
+
continue;
|
|
786
|
+
}
|
|
787
|
+
if (rest.includes("/")) {
|
|
788
|
+
issues.push({ file: post.file, message: `${where}link to ${href} goes nowhere` });
|
|
779
789
|
continue;
|
|
780
790
|
}
|
|
781
791
|
if (published.has(rest)) continue;
|
|
@@ -832,6 +842,43 @@ function renderToc(toc) {
|
|
|
832
842
|
).join("");
|
|
833
843
|
return `<nav class="toc" aria-label="On this page"><p class="toc-title">On this page</p><ol>${items}</ol></nav>`;
|
|
834
844
|
}
|
|
845
|
+
function usedTags(content) {
|
|
846
|
+
const used = new Set(content.posts.flatMap((post) => post.tags));
|
|
847
|
+
const known = Object.keys(content.tags).filter((tag) => used.has(tag));
|
|
848
|
+
const others = [...used].filter((tag) => !Object.hasOwn(content.tags, tag)).sort();
|
|
849
|
+
return [...known, ...others];
|
|
850
|
+
}
|
|
851
|
+
function listPath(options, tag) {
|
|
852
|
+
return tag ? `${options.base}tag/${tag}/` : options.base;
|
|
853
|
+
}
|
|
854
|
+
function listPages(options, content) {
|
|
855
|
+
const page = (sitePath) => ({
|
|
856
|
+
url: joinUrl(options.siteBase, sitePath),
|
|
857
|
+
canonical: options.site ? joinUrl(options.site, joinUrl(options.siteBase, sitePath)) : void 0
|
|
858
|
+
});
|
|
859
|
+
const pages = [
|
|
860
|
+
{
|
|
861
|
+
kind: "index",
|
|
862
|
+
...page(listPath(options)),
|
|
863
|
+
title: options.index?.title ?? "Blog",
|
|
864
|
+
description: options.index?.description ?? "",
|
|
865
|
+
posts: content.posts
|
|
866
|
+
}
|
|
867
|
+
];
|
|
868
|
+
if (options.tagPages === false) return pages;
|
|
869
|
+
for (const tag of usedTags(content)) {
|
|
870
|
+
const info = content.tags[tag];
|
|
871
|
+
pages.push({
|
|
872
|
+
kind: "tag",
|
|
873
|
+
...page(listPath(options, tag)),
|
|
874
|
+
title: info?.name ?? tag,
|
|
875
|
+
description: info?.description ?? "",
|
|
876
|
+
tag,
|
|
877
|
+
posts: content.posts.filter((post) => post.tags.includes(tag))
|
|
878
|
+
});
|
|
879
|
+
}
|
|
880
|
+
return pages;
|
|
881
|
+
}
|
|
835
882
|
function createHelpers(options, content) {
|
|
836
883
|
const display = {
|
|
837
884
|
locale: options.locale,
|
|
@@ -842,6 +889,13 @@ function createHelpers(options, content) {
|
|
|
842
889
|
};
|
|
843
890
|
const formatDate = (date, hasTime = false) => formatPostDate({ date, hasTime }, display);
|
|
844
891
|
const dateAttribute = (date, hasTime = false) => toDateTimeAttribute({ date, hasTime }, options.timeZone);
|
|
892
|
+
const tags = usedTags(content);
|
|
893
|
+
const indexUrl = joinUrl(options.siteBase, listPath(options));
|
|
894
|
+
const tagUrl = (tag) => joinUrl(options.siteBase, listPath(options, tag));
|
|
895
|
+
const tagItem = (tag) => {
|
|
896
|
+
const name = escapeHtml(content.tags[tag]?.name ?? tag);
|
|
897
|
+
return options.tagPages === false ? `<li class="tag">${name}</li>` : `<li class="tag"><a href="${escapeHtml(tagUrl(tag))}">${name}</a></li>`;
|
|
898
|
+
};
|
|
845
899
|
const renderHead = (post) => [
|
|
846
900
|
`<title>${escapeHtml(post.title + options.titleSuffix)}</title>`,
|
|
847
901
|
meta("name", "description", post.description),
|
|
@@ -856,7 +910,7 @@ function createHelpers(options, content) {
|
|
|
856
910
|
...post.tags.map((tag) => meta("property", "article:tag", content.tags[tag]?.name ?? tag))
|
|
857
911
|
].filter(Boolean).join("\n ");
|
|
858
912
|
const renderArticle = (post) => {
|
|
859
|
-
const
|
|
913
|
+
const tags2 = post.tags.map(tagItem).join("");
|
|
860
914
|
const updated = post.updated ? ` \xB7 Updated <time datetime="${dateAttribute(post.updated)}">${formatDate(post.updated)}</time>` : "";
|
|
861
915
|
return [
|
|
862
916
|
'<article class="post">',
|
|
@@ -864,7 +918,7 @@ function createHelpers(options, content) {
|
|
|
864
918
|
`<h1>${escapeHtml(post.title)}</h1>`,
|
|
865
919
|
`<p class="post-description">${escapeHtml(post.description)}</p>`,
|
|
866
920
|
`<p class="post-meta"><time datetime="${dateAttribute(post.date, post.dateHasTime)}">${formatDate(post.date, post.dateHasTime)}</time>${updated} \xB7 ${post.readingMinutes} min read</p>`,
|
|
867
|
-
|
|
921
|
+
tags2 ? `<ul class="post-tags">${tags2}</ul>` : "",
|
|
868
922
|
"</header>",
|
|
869
923
|
renderToc(post.toc),
|
|
870
924
|
`<div class="prose" data-pagefind-body>
|
|
@@ -872,7 +926,52 @@ ${post.html}</div>`,
|
|
|
872
926
|
"</article>"
|
|
873
927
|
].filter(Boolean).join("\n");
|
|
874
928
|
};
|
|
929
|
+
const indexTitle = options.index?.title ?? "Blog";
|
|
930
|
+
const renderListHead = (page) => {
|
|
931
|
+
const title = page.kind === "index" ? page.title : `${page.title} \xB7 ${indexTitle}`;
|
|
932
|
+
return [
|
|
933
|
+
`<title>${escapeHtml(title + options.titleSuffix)}</title>`,
|
|
934
|
+
meta("name", "description", page.description),
|
|
935
|
+
page.canonical ? `<link rel="canonical" href="${escapeHtml(page.canonical)}">` : "",
|
|
936
|
+
meta("property", "og:type", "website"),
|
|
937
|
+
meta("property", "og:title", title),
|
|
938
|
+
meta("property", "og:description", page.description),
|
|
939
|
+
meta("property", "og:url", page.canonical)
|
|
940
|
+
].filter(Boolean).join("\n ");
|
|
941
|
+
};
|
|
942
|
+
const renderList = (page) => {
|
|
943
|
+
const items = page.posts.map((post) => {
|
|
944
|
+
const postTags = post.tags.map(tagItem).join("");
|
|
945
|
+
return [
|
|
946
|
+
'<li class="post-item">',
|
|
947
|
+
`<h2><a href="${escapeHtml(post.url)}">${escapeHtml(post.title)}</a></h2>`,
|
|
948
|
+
`<p class="post-meta"><time datetime="${dateAttribute(post.date, post.dateHasTime)}">${formatDate(post.date, post.dateHasTime)}</time> \xB7 ${post.readingMinutes} min read</p>`,
|
|
949
|
+
`<p class="post-description">${escapeHtml(post.description)}</p>`,
|
|
950
|
+
postTags ? `<ul class="post-tags">${postTags}</ul>` : "",
|
|
951
|
+
"</li>"
|
|
952
|
+
].filter(Boolean).join("");
|
|
953
|
+
}).join("\n");
|
|
954
|
+
const tagLinks = options.tagPages === false ? "" : tags.map((tag) => {
|
|
955
|
+
const current = page.tag === tag ? ' aria-current="page"' : "";
|
|
956
|
+
return `<li><a href="${escapeHtml(tagUrl(tag))}"${current}>${escapeHtml(content.tags[tag]?.name ?? tag)}</a></li>`;
|
|
957
|
+
}).join("");
|
|
958
|
+
return [
|
|
959
|
+
`<section class="post-list" aria-labelledby="post-list-title">`,
|
|
960
|
+
`<h1 id="post-list-title">${escapeHtml(page.title)}</h1>`,
|
|
961
|
+
page.description ? `<p class="post-list-description">${escapeHtml(page.description)}</p>` : "",
|
|
962
|
+
tagLinks ? `<nav class="post-list-tags" aria-label="Tags"><ul>${tagLinks}</ul></nav>` : "",
|
|
963
|
+
items ? `<ul class="post-items">
|
|
964
|
+
${items}
|
|
965
|
+
</ul>` : '<p class="post-list-empty">No posts yet.</p>',
|
|
966
|
+
"</section>"
|
|
967
|
+
].filter(Boolean).join("\n");
|
|
968
|
+
};
|
|
875
969
|
return {
|
|
970
|
+
indexUrl,
|
|
971
|
+
tagUrl,
|
|
972
|
+
usedTags: tags,
|
|
973
|
+
renderListHead,
|
|
974
|
+
renderList,
|
|
876
975
|
escapeHtml,
|
|
877
976
|
formatDate,
|
|
878
977
|
dateAttribute,
|
|
@@ -893,6 +992,11 @@ function renderPostPage(template, post, options, helpers) {
|
|
|
893
992
|
const article = options.render?.article?.(post, helpers) ?? helpers.renderArticle(post);
|
|
894
993
|
return fillTemplate(template, head, article);
|
|
895
994
|
}
|
|
995
|
+
function renderListPage(template, page, options, helpers) {
|
|
996
|
+
const head = options.render?.listHead?.(page, helpers) ?? helpers.renderListHead(page);
|
|
997
|
+
const body = options.render?.list?.(page, helpers) ?? helpers.renderList(page);
|
|
998
|
+
return fillTemplate(template, head, body);
|
|
999
|
+
}
|
|
896
1000
|
function postSummaries(posts) {
|
|
897
1001
|
return posts.map((post) => ({
|
|
898
1002
|
slug: post.slug,
|
|
@@ -930,7 +1034,17 @@ function blog(userOptions) {
|
|
|
930
1034
|
let load;
|
|
931
1035
|
let content;
|
|
932
1036
|
const pageIds = /* @__PURE__ */ new Map();
|
|
933
|
-
const
|
|
1037
|
+
const fileFor = (sitePath) => path3.join(config.root, sitePath.slice(1), "index.html");
|
|
1038
|
+
const pageFile = (slug) => fileFor(postPath(options, slug));
|
|
1039
|
+
const allPages = () => {
|
|
1040
|
+
const pages = /* @__PURE__ */ new Map();
|
|
1041
|
+
for (const page of listPages(options, content)) {
|
|
1042
|
+
pages.set(page.url.slice(options.siteBase.length - 1), { kind: "list", page });
|
|
1043
|
+
}
|
|
1044
|
+
for (const post of content.posts)
|
|
1045
|
+
pages.set(postPath(options, post.slug), { kind: "post", post });
|
|
1046
|
+
return pages;
|
|
1047
|
+
};
|
|
934
1048
|
const assetUrl = (file, slug) => config.command === "serve" ? `/@fs/${toPosix(file).replace(/^\/+/, "")}` : toPosix(path3.relative(path3.dirname(pageFile(slug)), file));
|
|
935
1049
|
const readTemplate = () => {
|
|
936
1050
|
const template = readFileSync3(options.template, "utf8");
|
|
@@ -940,7 +1054,10 @@ function blog(userOptions) {
|
|
|
940
1054
|
);
|
|
941
1055
|
return template;
|
|
942
1056
|
};
|
|
943
|
-
const renderPage = (
|
|
1057
|
+
const renderPage = (page) => {
|
|
1058
|
+
const helpers = createHelpers(options, content);
|
|
1059
|
+
return page.kind === "post" ? renderPostPage(readTemplate(), page.post, options, helpers) : renderListPage(readTemplate(), page.page, options, helpers);
|
|
1060
|
+
};
|
|
944
1061
|
return {
|
|
945
1062
|
name: "vite-plugin-blog",
|
|
946
1063
|
enforce: "pre",
|
|
@@ -960,10 +1077,11 @@ function blog(userOptions) {
|
|
|
960
1077
|
if (config.command !== "build") return;
|
|
961
1078
|
pageIds.clear();
|
|
962
1079
|
const pages = {};
|
|
963
|
-
for (const
|
|
964
|
-
const id =
|
|
965
|
-
pageIds.set(id,
|
|
966
|
-
|
|
1080
|
+
for (const [sitePath, page] of allPages()) {
|
|
1081
|
+
const id = fileFor(sitePath);
|
|
1082
|
+
pageIds.set(id, page);
|
|
1083
|
+
const rest = sitePath.slice(options.base.length).replace(/\/$/, "");
|
|
1084
|
+
pages[rest ? `blog-${rest.replace(/^tag\//, "tag__")}` : "blog"] = id;
|
|
967
1085
|
}
|
|
968
1086
|
return { ...inputOptions, input: addInputs(inputOptions.input, pages) };
|
|
969
1087
|
},
|
|
@@ -979,8 +1097,8 @@ function blog(userOptions) {
|
|
|
979
1097
|
export default posts;
|
|
980
1098
|
`;
|
|
981
1099
|
if (id === RESOLVED_CSS_ID) return content.css;
|
|
982
|
-
const
|
|
983
|
-
if (
|
|
1100
|
+
const page = pageIds.get(id);
|
|
1101
|
+
if (page) return renderPage(page);
|
|
984
1102
|
return null;
|
|
985
1103
|
},
|
|
986
1104
|
configureServer(server) {
|
|
@@ -1006,19 +1124,23 @@ export default posts;
|
|
|
1006
1124
|
server.middlewares.use(async (req, res, next) => {
|
|
1007
1125
|
try {
|
|
1008
1126
|
const url = new URL(req.url ?? "/", "http://localhost");
|
|
1009
|
-
const
|
|
1010
|
-
if (!url.pathname.startsWith(
|
|
1011
|
-
const
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1127
|
+
const siteBase = options.siteBase.replace(/\/$/, "");
|
|
1128
|
+
if (!url.pathname.startsWith(siteBase)) return next();
|
|
1129
|
+
const sitePath = url.pathname.slice(siteBase.length).replace(/\/index\.html$/, "/");
|
|
1130
|
+
if (!sitePath.startsWith(options.base.replace(/\/$/, ""))) return next();
|
|
1131
|
+
const pages = allPages();
|
|
1132
|
+
const page = pages.get(sitePath);
|
|
1133
|
+
if (!page) {
|
|
1134
|
+
if (!sitePath.endsWith("/") && pages.has(`${sitePath}/`)) {
|
|
1135
|
+
res.statusCode = 301;
|
|
1136
|
+
res.setHeader("Location", `${siteBase}${sitePath}/${url.search}`);
|
|
1137
|
+
return res.end();
|
|
1138
|
+
}
|
|
1139
|
+
return next();
|
|
1018
1140
|
}
|
|
1019
1141
|
const html = await server.transformIndexHtml(
|
|
1020
|
-
`${
|
|
1021
|
-
renderPage(
|
|
1142
|
+
`${sitePath}index.html`,
|
|
1143
|
+
renderPage(page),
|
|
1022
1144
|
req.originalUrl
|
|
1023
1145
|
);
|
|
1024
1146
|
res.setHeader("Content-Type", "text/html; charset=utf-8");
|