fumadocs-mdx
Version:
The built-in source for Fumadocs
81 lines (79 loc) • 2.15 kB
JavaScript
import { n as ident } from "../codegen-DleOVLNr.js";
import path from "node:path";
import { x } from "tinyexec";
//#region src/plugins/last-modified.ts
const cache = /* @__PURE__ */ new Map();
const ExtendTypes = `{
/**
* Last modified date of document file, obtained from version control.
*
*/
lastModified?: Date;
}`;
/**
* Injects `lastModified` property to page exports.
*/
function lastModified(options = {}) {
const { versionControl = "git", filter = () => true } = options;
let fn;
return {
name: "last-modified",
"index-file": {
generateTypeConfig() {
const lines = [];
lines.push("{");
lines.push(" DocData: {");
for (const collection of this.core.getCollections()) if (filter(collection.name)) lines.push(ident(`${collection.name}: ${ExtendTypes},`, 2));
lines.push(" }");
lines.push("}");
return lines.join("\n");
},
serverOptions(options$1) {
options$1.doc ??= {};
options$1.doc.passthroughs ??= [];
options$1.doc.passthroughs.push("lastModified");
}
},
config() {
const { workspace } = this.core.getOptions();
const cwd = workspace ? path.resolve(workspace.dir) : process.cwd();
switch (versionControl) {
case "git":
fn = (v) => getGitTimestamp(v, cwd);
break;
default: fn = versionControl;
}
},
doc: { async vfile(file) {
if (!filter(this.collection.name)) return;
const timestamp = await fn(this.filePath);
if (timestamp) {
file.data["mdx-export"] ??= [];
file.data["mdx-export"].push({
name: "lastModified",
value: timestamp
});
}
} }
};
}
async function getGitTimestamp(file, cwd) {
const cached = cache.get(file);
if (cached) return cached;
const timePromise = (async () => {
const out = await x("git", [
"log",
"-1",
"--pretty=\"%ai\"",
path.relative(cwd, file)
], { nodeOptions: { cwd } });
if (out.exitCode !== 0) return null;
const date = new Date(out.stdout);
return isNaN(date.getTime()) ? null : date;
})();
cache.set(file, timePromise);
return timePromise;
}
//#endregion
export { lastModified as default };
//# sourceMappingURL=last-modified.js.map