fumadocs-core
Version:
The library for building a documentation website in any React.js framework
35 lines (33 loc) • 955 B
JavaScript
// src/negotiation/index.ts
import Negotiator from "negotiator";
import { compile, match } from "path-to-regexp";
function getNegotiator(request) {
const headers = {};
request.headers.forEach((value, key) => {
headers[key] = value;
});
return new Negotiator({ headers });
}
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));
}
export {
getNegotiator,
rewritePath,
isMarkdownPreferred
};