@docuify/engine
Version:
A flexible, pluggable engine for building and transforming documentation content from source files.
154 lines (148 loc) • 5.2 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// lib/sources/index.ts
var sources_exports = {};
__export(sources_exports, {
Github: () => Github,
LocalFile: () => LocalFile
});
module.exports = __toCommonJS(sources_exports);
// lib/base/baseSource.ts
var BaseSource = class {
};
// lib/utils/extract_file_ext.ts
function getFileExtension(filename) {
const match = filename.match(/(?:\.([^.\/\\]+))?$/);
return match?.[1] ?? null;
}
// lib/sources/github.ts
var Github = class extends BaseSource {
name = "Github";
config;
constructor(config) {
super();
if (!config.token || !config.branch || !config.repoFullName) {
throw new Error("Invalid config passed to Github source.");
}
this.config = config;
}
async fetch() {
const data = await this.request();
const parsedData = await this.parse(data.tree);
return parsedData;
}
get requestHeaders() {
return {
Authorization: `Bearer ${this.config.token}`,
"X-GitHub-Api-Version": this.config.github_api_version ?? "2022-11-28"
};
}
async parse(data) {
const fields = this.config.metadataFields || ["sha"];
return data.map((item) => {
const isFolder = item.type === "tree";
const metadata = {};
for (const field of fields) {
if (field in item) {
metadata[field] = item[field];
}
}
const file = {
path: item.path,
type: isFolder ? "folder" : "file",
metadata,
extension: isFolder ? null : getFileExtension(item.path),
loadContent: isFolder ? void 0 : async () => this.fetchFileContent(item.path)
};
return file;
});
}
async fetchFileContent(path2) {
const url = `https://raw.githubusercontent.com/${this.config.repoFullName}/${this.config.branch}/${path2}`;
const res = await fetch(url, { headers: this.requestHeaders });
if (!res.ok) {
throw new Error(
`Failed to fetch file content from GitHub: ${res.status} ${res.statusText}`
);
}
return await res.text();
}
async request() {
const url = `https://api.github.com/repos/${this.config.repoFullName}/git/trees/${this.config.branch}?recursive=1`;
const res = await fetch(url, { headers: this.requestHeaders });
if (!res.ok) {
throw new Error(
`Failed to fetch data from GitHub: ${res.status} ${res.statusText}`
);
}
return await res.json();
}
};
// lib/sources/localfile.ts
var import_promises = __toESM(require("fs/promises"));
var import_path = __toESM(require("path"));
var LocalFile = class extends BaseSource {
constructor(rootDir) {
super();
this.rootDir = rootDir;
this.rootDir = import_path.default.resolve(process.cwd(), rootDir);
}
name = "local-file-source";
// Recursively fetch files from the local directory
async fetch() {
const files = [];
await this._readDirRecursive(this.rootDir, files);
return files;
}
async _readDirRecursive(dir, files, parentPath = "") {
const entries = await import_promises.default.readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = import_path.default.join(dir, entry.name);
const relativePath = parentPath ? `${parentPath}/${entry.name}` : entry.name;
if (entry.isDirectory()) {
await this._readDirRecursive(fullPath, files, relativePath);
} else if (entry.isFile()) {
files.push({
path: relativePath,
type: "file",
extension: import_path.default.extname(entry.name).slice(1),
// remove dot
loadContent: async () => {
return import_promises.default.readFile(fullPath, "utf-8");
}
});
}
}
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Github,
LocalFile
});