UNPKG

@mintlify/link-rot

Version:

Static checking for broken internal links

80 lines (79 loc) 3.92 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { coreRemark } from '@mintlify/common'; import fs from 'fs-extra'; import path from 'path'; import { visit } from 'unist-util-visit'; import { Graph, Wrapper } from '../graph.js'; import { getLinkPaths, getPagePaths } from '../prebuild.js'; export const decorateGraphNodeFromPageContent = (graphNode, content) => __awaiter(void 0, void 0, void 0, function* () { const visitLinks = () => { return (tree) => { visit(tree, (node) => { var _a, _b; if ( // ![]() format node.type === 'link' || node.type === 'image') { graphNode.addPath(node.url, Wrapper.MD); return; } const mdxJsxFlowElement = node; if (mdxJsxFlowElement.name === 'img' || mdxJsxFlowElement.name === 'source') { const srcAttrIndex = mdxJsxFlowElement.attributes.findIndex((attr) => attr.type === 'mdxJsxAttribute' && attr.name === 'src'); const nodeUrl = (_a = mdxJsxFlowElement.attributes[srcAttrIndex]) === null || _a === void 0 ? void 0 : _a.value; if (typeof nodeUrl === 'string') { graphNode.addPath(nodeUrl, Wrapper.SRC); return; } } else if (mdxJsxFlowElement.name === 'a' || mdxJsxFlowElement.name === 'Card') { const hrefAttrIndex = mdxJsxFlowElement.attributes.findIndex((attr) => attr.type === 'mdxJsxAttribute' && attr.name === 'href'); const nodeUrl = (_b = mdxJsxFlowElement.attributes[hrefAttrIndex]) === null || _b === void 0 ? void 0 : _b.value; if (typeof nodeUrl === 'string') { graphNode.addPath(nodeUrl, Wrapper.HREF); return; } } }); return tree; }; }; yield coreRemark().use(visitLinks).process(content); return; }); /** * Get all broken internal links used in the site */ export const getBrokenInternalLinks = (repoPath) => __awaiter(void 0, void 0, void 0, function* () { const baseDir = repoPath ? repoPath : process.cwd(); const graph = new Graph(baseDir); // add nodes for every page or media path in project const filenames = getLinkPaths(baseDir); graph.addNodes(filenames); // add nodes for every link alias in the project graph.addAliasNodes(); const sitePages = getPagePaths(baseDir); yield Promise.all(sitePages.map((filePath) => __awaiter(void 0, void 0, void 0, function* () { const fileNode = graph.getNode(filePath); if (fileNode) { const fileContent = fs.readFileSync(path.join(baseDir, filePath)).toString(); try { yield decorateGraphNodeFromPageContent(fileNode, fileContent); } catch (err) { throw new Error(`Syntax error - Unable to parse ${filePath} - ${err}`); } } }))); // Precompute file resolutions before checking for broken links graph.precomputeFileResolutions(); return graph.getBrokenInternalLinks(); });