@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
212 lines • 5.79 kB
JavaScript
import _replaceAllInstanceProperty from "core-js-pure/stable/instance/replace-all.js";
import _pushInstanceProperty from "core-js-pure/stable/instance/push.js";
export function normalizeDocsPath(input) {
var _context;
const raw = _replaceAllInstanceProperty(_context = String(input !== null && input !== void 0 ? input : '').replace(/^\/+/, '')).call(_context, '\\', '/');
const segments = [];
for (const segment of raw.split('/')) {
if (segment === '' || segment === '.') {
continue;
}
if (segment === '..') {
throw new Error(`Path escapes docs root: ${String(input)}`);
}
_pushInstanceProperty(segments).call(segments, segment);
}
return segments.join('/');
}
export function createBundledDocsSource(bundle, options = {}) {
var _options$label;
const files = new Map();
for (const [rawKey, value] of Object.entries(bundle)) {
files.set(normalizeDocsPath(rawKey), value);
}
const dirs = new Set();
files.forEach((_value, key) => {
const parts = key.split('/');
for (let i = 1; i < parts.length; i++) {
dirs.add(parts.slice(0, i).join('/'));
}
});
dirs.add('');
const markdown = Array.from(files.keys()).filter(p => p.endsWith('.md') || p.endsWith('.mdx')).sort();
return {
label: (_options$label = options.label) !== null && _options$label !== void 0 ? _options$label : 'bundle',
async listMarkdown() {
return markdown;
},
async read(relPath) {
const key = normalizeDocsPath(relPath);
const value = files.get(key);
return typeof value === 'string' ? value : null;
},
async stat(relPath) {
const key = normalizeDocsPath(relPath);
if (files.has(key)) {
return {
kind: 'file'
};
}
if (dirs.has(key)) {
return {
kind: 'dir'
};
}
return {
kind: 'missing'
};
},
async listDir(relPath, max = 60) {
const key = normalizeDocsPath(relPath);
if (!dirs.has(key)) {
return [];
}
const prefix = key === '' ? '' : `${key}/`;
const seen = new Set();
files.forEach((_value, filePath) => {
if (!filePath.startsWith(prefix)) {
return;
}
const rest = filePath.slice(prefix.length);
const next = rest.split('/')[0];
if (next) {
seen.add(next);
}
});
dirs.forEach(dirPath => {
if (dirPath === key || dirPath === '') {
return;
}
if (!dirPath.startsWith(prefix)) {
return;
}
const rest = dirPath.slice(prefix.length);
const next = rest.split('/')[0];
if (next) {
seen.add(next);
}
});
return Array.from(seen).sort().slice(0, max);
}
};
}
export async function createNodeDocsSource(rootAbs) {
const [{
default: fs
}, {
default: path
}] = await Promise.all([import('node:fs/promises'), import('node:path')]);
const root = path.resolve(rootAbs);
function resolveInside(relPath) {
const cleaned = normalizeDocsPath(relPath);
const abs = path.resolve(root, cleaned);
const rel = path.relative(root, abs);
if (rel.startsWith('..') || path.isAbsolute(rel)) {
throw new Error(`Path escapes docs root: ${relPath}`);
}
return abs;
}
async function listMarkdown() {
const out = [];
const stack = [''];
while (stack.length > 0) {
var _stack$pop;
const relDir = (_stack$pop = stack.pop()) !== null && _stack$pop !== void 0 ? _stack$pop : '';
const absDir = path.join(root, relDir);
let entries;
try {
entries = await fs.readdir(absDir, {
withFileTypes: true
});
} catch {
continue;
}
for (const entry of entries) {
if (entry.name.startsWith('.')) {
continue;
}
const relPath = path.join(relDir, entry.name);
if (entry.isDirectory()) {
if (entry.name === 'node_modules') {
continue;
}
_pushInstanceProperty(stack).call(stack, relPath);
continue;
}
if (entry.isFile() && (entry.name.toLowerCase().endsWith('.md') || entry.name.toLowerCase().endsWith('.mdx'))) {
_pushInstanceProperty(out).call(out, _replaceAllInstanceProperty(relPath).call(relPath, path.sep, '/'));
}
}
}
return out;
}
async function statSafe(abs) {
try {
return await fs.stat(abs);
} catch {
return null;
}
}
return {
label: `node:${root}`,
listMarkdown,
async read(relPath) {
let abs;
try {
abs = resolveInside(relPath);
} catch {
return null;
}
const st = await statSafe(abs);
if (!(st !== null && st !== void 0 && st.isFile())) {
return null;
}
const buf = await fs.readFile(abs);
return buf.toString('utf8');
},
async stat(relPath) {
let abs;
try {
abs = resolveInside(relPath);
} catch {
return {
kind: 'missing'
};
}
const st = await statSafe(abs);
if (!st) {
return {
kind: 'missing'
};
}
if (st.isFile()) {
return {
kind: 'file'
};
}
if (st.isDirectory()) {
return {
kind: 'dir'
};
}
return {
kind: 'missing'
};
},
async listDir(relPath, max = 60) {
let abs;
try {
abs = resolveInside(relPath);
} catch {
return [];
}
try {
const items = await fs.readdir(abs);
return items.slice(0, max);
} catch {
return [];
}
}
};
}
//# sourceMappingURL=docs-source.js.map