fumadocs-core
Version:
The React.js library for building a documentation website
41 lines (39 loc) • 1.28 kB
JavaScript
import Negotiator from "negotiator";
import { compile, match } from "path-to-regexp";
//#region src/negotiation/index.ts
function getNegotiator(request) {
const headers = {};
request.headers.forEach((value, key) => {
headers[key] = value;
});
return new Negotiator({ headers });
}
/**
* Rewrite incoming path matching the `source` pattern into the `destination` pattern.
*
* See [`path-to-regexp`](https://github.com/pillarjs/path-to-regexp) for accepted pattern formats.
*
* @param source - the original pattern of incoming paths
* @param destination - the target pattern to convert into
*/
function rewritePath(source, destination) {
const matcher = match(source, { decode: false });
const compiler = compile(destination, { encode: false });
return { rewrite(pathname) {
const result = matcher(pathname);
if (!result) return false;
return compiler(result.params);
} };
}
function isMarkdownPreferred(request, options) {
const { markdownMediaTypes = [
"text/plain",
"text/markdown",
"text/x-markdown"
] } = options ?? {};
const mediaTypes = getNegotiator(request).mediaTypes();
return markdownMediaTypes.some((type) => mediaTypes.includes(type));
}
//#endregion
export { getNegotiator, isMarkdownPreferred, rewritePath };
//# sourceMappingURL=index.js.map