@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
65 lines • 1.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sanitize = sanitize;
exports.addContextRoot = addContextRoot;
exports.makePermalink = makePermalink;
exports.dir = dir;
/**
* Sanitize a string for use in URLs
*/
function sanitize(input) {
return input
.replace(/[\s\t\n\r]+/g, '-') // Replace whitespace with hyphens
.replace(/[^\w\-_]/g, '') // Remove special characters
.replace(/-+/g, '-') // Replace multiple hyphens with single hyphen
.replace(/^-|-$/g, ''); // Remove leading/trailing hyphens
}
/**
* Add context root to a path
*/
function addContextRoot(root, path) {
if (!root || path.startsWith(root)) {
return path;
}
return joinPaths(root, path);
}
/**
* Create a permalink by joining base URL and path
*/
function makePermalink(baseURL, path) {
// Remove trailing slashes from baseURL and leading slashes from path
const base = baseURL.replace(/\/$/, '');
const cleanPath = path.replace(/^\//, '');
// Join with a single slash
const fullURL = `${base}/${cleanPath}`;
try {
return new URL(fullURL);
}
catch {
// If the URL is invalid, try to make it valid
return new URL('http://' + fullURL);
}
}
/**
* Join path segments with a single slash
*/
function joinPaths(...paths) {
return paths
.filter(Boolean)
.join('/')
.replace(/\/+/g, '/')
.replace(/\/$/, '');
}
function dir(s) {
const lastSlashIndex = s.lastIndexOf('/');
if (lastSlashIndex === -1) {
return '';
}
let dir = s.slice(0, lastSlashIndex + 1); // path.Split 返回含末尾斜杠的 dir
// 去掉末尾斜杠(除了根目录 "/")
if (dir.length > 1 && dir.endsWith('/')) {
dir = dir.slice(0, -1);
}
return dir;
}
//# sourceMappingURL=index.js.map