dumi
Version:
📖 Documentation Generator of React Component
114 lines (112 loc) • 4.82 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);
// src/loaders/markdown/transformer/rehypeLink.ts
var rehypeLink_exports = {};
__export(rehypeLink_exports, {
default: () => rehypeLink
});
module.exports = __toCommonJS(rehypeLink_exports);
var import_utils = require("../../../features/tabs/utils");
var import_utils2 = require("@umijs/utils");
var import_path = __toESM(require("path"));
var import_url = __toESM(require("url"));
var visit;
var SKIP;
(async () => {
({ visit, SKIP } = await import("unist-util-visit"));
})();
function createRouteInfo(routes, hostAbsPath) {
const pathsByFile = /* @__PURE__ */ new Map();
const absPaths = /* @__PURE__ */ new Set();
let currentRoute;
Object.values(routes).forEach((route) => {
pathsByFile.set(route.file, route.absPath);
absPaths.add(route.absPath);
if (!currentRoute && route.file === hostAbsPath)
currentRoute = route;
});
return { pathsByFile, absPaths, currentRoute };
}
function rehypeLink(opts) {
return (tree) => {
const hostAbsPath = (0, import_utils.getHostForTabRouteFile)(opts.fileAbsPath);
let routeInfo;
visit(tree, "element", (node, i, parent) => {
var _a, _b, _c;
if (node.tagName === "a" && typeof ((_a = node.properties) == null ? void 0 : _a.href) === "string" && // skip target specified link
!((_b = node.properties) == null ? void 0 : _b.target) && // skip download link
!((_c = node.properties) == null ? void 0 : _c.download)) {
const href = node.properties.href;
const parsedUrl = import_url.default.parse(href);
if (parsedUrl.protocol || href.startsWith("//"))
return SKIP;
if (/\.md$/i.test(parsedUrl.pathname)) {
const { pathsByFile } = routeInfo ?? (routeInfo = createRouteInfo(
opts.routes,
hostAbsPath
));
const absPath = (0, import_utils2.winPath)(
import_path.default.resolve(hostAbsPath, "..", parsedUrl.pathname)
);
if (pathsByFile.has(absPath)) {
parsedUrl.pathname = pathsByFile.get(absPath);
}
} else if (parsedUrl.pathname && /^[^/]+/.test(parsedUrl.pathname)) {
const { currentRoute, absPaths } = routeInfo ?? (routeInfo = createRouteInfo(
opts.routes,
hostAbsPath
));
if (!currentRoute)
return;
const basePath = currentRoute.absPath;
const htmlTargetPath = import_url.default.resolve(basePath, parsedUrl.pathname);
const rr6TargetPath = (0, import_utils2.winPath)(
import_path.default.resolve(basePath, parsedUrl.pathname)
);
parsedUrl.pathname = htmlTargetPath;
if (!absPaths.has(htmlTargetPath) && absPaths.has(rr6TargetPath)) {
parsedUrl.pathname = rr6TargetPath;
import_utils2.logger.warn(
`Detected ambiguous link \`${href}\` in \`${opts.fileAbsPath}\`, please use \`./xxx.md\` file path instead of normal relative path, dumi will deprecate this behavior in the future.
See more: https://github.com/umijs/dumi/pull/1491`
);
}
}
parent.children.splice(i, 1, {
type: "element",
tagName: "Link",
children: node.children,
properties: {
...import_utils2.lodash.omit(node.properties, ["href"]),
to: import_url.default.format(parsedUrl)
}
});
}
});
};
}