@mintlify/link-rot 3.0.4 → 3.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,93 @@
1
+ Copyright (c) 2022 Mintlify, Inc.
2
+
3
+ Elastic License 2.0
4
+
5
+ ## Acceptance
6
+
7
+ By using the software, you agree to all of the terms and conditions below.
8
+
9
+ ## Copyright License
10
+
11
+ The licensor grants you a non-exclusive, royalty-free, worldwide,
12
+ non-sublicensable, non-transferable license to use, copy, distribute, make
13
+ available, and prepare derivative works of the software, in each case subject to
14
+ the limitations and conditions below.
15
+
16
+ ## Limitations
17
+
18
+ You may not provide the software to third parties as a hosted or managed
19
+ service, where the service provides users with access to any substantial set of
20
+ the features or functionality of the software.
21
+
22
+ You may not move, change, disable, or circumvent the license key functionality
23
+ in the software, and you may not remove or obscure any functionality in the
24
+ software that is protected by the license key.
25
+
26
+ You may not alter, remove, or obscure any licensing, copyright, or other notices
27
+ of the licensor in the software. Any use of the licensor’s trademarks is subject
28
+ to applicable law.
29
+
30
+ ## Patents
31
+
32
+ The licensor grants you a license, under any patent claims the licensor can
33
+ license, or becomes able to license, to make, have made, use, sell, offer for
34
+ sale, import and have imported the software, in each case subject to the
35
+ limitations and conditions in this license. This license does not cover any
36
+ patent claims that you cause to be infringed by modifications or additions to
37
+ the software. If you or your company make any written claim that the software
38
+ infringes or contributes to infringement of any patent, your patent license for
39
+ the software granted under these terms ends immediately. If your company makes
40
+ such a claim, your patent license ends immediately for work on behalf of your
41
+ company.
42
+
43
+ ## Notices
44
+
45
+ You must ensure that anyone who gets a copy of any part of the software from you
46
+ also gets a copy of these terms.
47
+
48
+ If you modify the software, you must include in any modified copies of the
49
+ software prominent notices stating that you have modified the software.
50
+
51
+ ## No Other Rights
52
+
53
+ These terms do not imply any licenses other than those expressly granted in
54
+ these terms.
55
+
56
+ ## Termination
57
+
58
+ If you use the software in violation of these terms, such use is not licensed,
59
+ and your licenses will automatically terminate. If the licensor provides you
60
+ with a notice of your violation, and you cease all violation of this license no
61
+ later than 30 days after you receive that notice, your licenses will be
62
+ reinstated retroactively. However, if you violate these terms after such
63
+ reinstatement, any additional violation of these terms will cause your licenses
64
+ to terminate automatically and permanently.
65
+
66
+ ## No Liability
67
+
68
+ _As far as the law allows, the software comes as is, without any warranty or
69
+ condition, and the licensor will not be liable to you for any damages arising
70
+ out of these terms or the use or nature of the software, under any kind of
71
+ legal claim._
72
+
73
+ ## Definitions
74
+
75
+ The **licensor** is the entity offering these terms, and the **software** is the
76
+ software the licensor makes available under these terms, including any portion
77
+ of it.
78
+
79
+ **you** refers to the individual or entity agreeing to these terms.
80
+
81
+ **your company** is any legal entity, sole proprietorship, or other kind of
82
+ organization that you work for, plus all organizations that have control over,
83
+ are under the control of, or are under common control with that
84
+ organization. **control** means ownership of substantially all the assets of an
85
+ entity, or the power to direct its management and policies by vote, contract, or
86
+ otherwise. Control can be direct or indirect.
87
+
88
+ **your licenses** are all the licenses granted to you for the software under
89
+ these terms.
90
+
91
+ **use** means anything you do with the software requiring one of your licenses.
92
+
93
+ **trademark** means trademarks, service marks, and similar rights.
@@ -0,0 +1,78 @@
1
+ /// <reference types="node" />
2
+ import { ParsedPath } from 'path';
3
+ declare enum PathType {
4
+ INTERNAL = "internal",
5
+ EXTERNAL = "external",
6
+ DATA = "data"
7
+ }
8
+ export declare enum Wrapper {
9
+ MD = "md",
10
+ SRC = "src",
11
+ HREF = "href",
12
+ CARD = "card"
13
+ }
14
+ export declare enum EdgeType {
15
+ CONTENT = "content",
16
+ NAVIGATION = "navigation"
17
+ }
18
+ /**
19
+ * An MdxPath is a path in an Mdx page. Contains all path-related information.
20
+ */
21
+ export declare class MdxPath {
22
+ originalPath: string;
23
+ relativeDir: string;
24
+ filename: string;
25
+ wrapper?: Wrapper | undefined;
26
+ path: ParsedPath | URL;
27
+ pathType: PathType;
28
+ anchorLink?: string;
29
+ queryParams?: URLSearchParams;
30
+ static getPathType(url: string): PathType;
31
+ /**
32
+ * Generate all the computed fields in an MdxPath
33
+ */
34
+ private constructParsedPath;
35
+ constructor(originalPath: string, relativeDir?: string, filename?: string, wrapper?: Wrapper | undefined);
36
+ toString(): string;
37
+ getResolvedFiles(allFiles: string[], baseDir: string): string[];
38
+ resolvesTo(node: Node, allFiles: string[], baseDir: string): boolean;
39
+ static filterMapInternalPaths(paths: string[]): string[];
40
+ }
41
+ export declare class Node {
42
+ label: string;
43
+ paths: MdxPath[];
44
+ relativeDir: string;
45
+ filename: string;
46
+ edges: Edge[];
47
+ constructor(label: string, paths?: MdxPath[]);
48
+ toString(): string;
49
+ equals(other: Node): boolean;
50
+ addPath(originalPath: string, wrapper?: Wrapper): void;
51
+ addOutgoingEdge(edge: Edge): void;
52
+ getChildNodes(depth?: number): Node[];
53
+ }
54
+ export declare class Edge {
55
+ source: Node;
56
+ target: Node;
57
+ edgeType: EdgeType;
58
+ private count;
59
+ constructor(source: Node, target: Node, edgeType: EdgeType);
60
+ incrementCount(): void;
61
+ getCount(): number;
62
+ equals(other: Edge): boolean;
63
+ }
64
+ export declare class Graph {
65
+ baseDir: string;
66
+ private nodes;
67
+ private edges;
68
+ constructor(baseDir: string);
69
+ addNode(label: string): Node;
70
+ addNodes(labels: string[]): void;
71
+ addAliasNodes(): void;
72
+ getNode(label: string): Node;
73
+ private addEdge;
74
+ addEdgesBetweenNodes(): void;
75
+ getBrokenInternalLinks(): MdxPath[];
76
+ getAllInternalPaths(): string[];
77
+ }
78
+ export {};
package/dist/graph.js ADDED
@@ -0,0 +1,302 @@
1
+ import { getFileListWithDirectories } from '@mintlify/prebuild';
2
+ import { existsSync } from 'fs';
3
+ import isAbsoluteUrl from 'is-absolute-url';
4
+ import { parse, join, resolve, relative, dirname, basename } from 'path';
5
+ import { sep as WINDOWS_SEPARATOR } from 'path/win32';
6
+ import { getLinkPaths, normalizeDir } from './prebuild.js';
7
+ var PathType;
8
+ (function (PathType) {
9
+ PathType["INTERNAL"] = "internal";
10
+ PathType["EXTERNAL"] = "external";
11
+ PathType["DATA"] = "data";
12
+ })(PathType || (PathType = {}));
13
+ export var Wrapper;
14
+ (function (Wrapper) {
15
+ Wrapper["MD"] = "md";
16
+ Wrapper["SRC"] = "src";
17
+ Wrapper["HREF"] = "href";
18
+ Wrapper["CARD"] = "card";
19
+ })(Wrapper || (Wrapper = {}));
20
+ export var EdgeType;
21
+ (function (EdgeType) {
22
+ EdgeType["CONTENT"] = "content";
23
+ EdgeType["NAVIGATION"] = "navigation";
24
+ })(EdgeType || (EdgeType = {}));
25
+ /**
26
+ * An MdxPath is a path in an Mdx page. Contains all path-related information.
27
+ */
28
+ export class MdxPath {
29
+ originalPath;
30
+ relativeDir;
31
+ filename;
32
+ wrapper;
33
+ path;
34
+ pathType;
35
+ anchorLink; // #anchor
36
+ queryParams; // ?query=param
37
+ static getPathType(url) {
38
+ let pathType;
39
+ if (isAbsoluteUrl(url)) {
40
+ pathType = PathType.EXTERNAL;
41
+ }
42
+ else if (url.startsWith('data:')) {
43
+ pathType = PathType.DATA;
44
+ }
45
+ else {
46
+ pathType = PathType.INTERNAL;
47
+ }
48
+ return pathType;
49
+ }
50
+ /**
51
+ * Generate all the computed fields in an MdxPath
52
+ */
53
+ constructParsedPath() {
54
+ const pathType = MdxPath.getPathType(this.originalPath);
55
+ let urlPath;
56
+ let path;
57
+ if (pathType === PathType.INTERNAL) {
58
+ let normalizedPath = this.originalPath.replaceAll(WINDOWS_SEPARATOR, '/');
59
+ if (normalizedPath.startsWith('.')) {
60
+ // also include paths that start with "..", etc.
61
+ normalizedPath = relative('.', resolve(this.relativeDir, normalizedPath));
62
+ // TODO path starts with .. if path resolves to file system outside of mintlify project
63
+ }
64
+ // remove leading slash
65
+ normalizedPath = normalizedPath.startsWith('/') ? normalizedPath.slice(1) : normalizedPath;
66
+ // resolve relative path
67
+ // add arbitrary protocol to parse as URL and isolate pathname
68
+ urlPath = new URL(`mintlify:${normalizedPath}`);
69
+ normalizedPath = urlPath.pathname;
70
+ // remove trailing slash
71
+ if (normalizedPath.endsWith('/')) {
72
+ normalizedPath = normalizedPath.slice(0, -1);
73
+ urlPath.pathname = normalizedPath;
74
+ }
75
+ // if path is "", then path points to same page
76
+ if (normalizedPath === '') {
77
+ normalizedPath = join(this.relativeDir, this.filename);
78
+ urlPath.pathname = normalizedPath;
79
+ }
80
+ path = parse(normalizedPath);
81
+ }
82
+ else {
83
+ urlPath = new URL(this.originalPath);
84
+ path = urlPath;
85
+ }
86
+ const anchorLink = urlPath.hash || undefined;
87
+ const queryParams = urlPath.searchParams || undefined;
88
+ return { path, pathType, anchorLink, queryParams };
89
+ }
90
+ constructor(originalPath, relativeDir = '', filename = '', wrapper) {
91
+ this.originalPath = originalPath;
92
+ this.relativeDir = relativeDir;
93
+ this.filename = filename;
94
+ this.wrapper = wrapper;
95
+ this.relativeDir = normalizeDir(relativeDir);
96
+ const { path, pathType, anchorLink, queryParams } = this.constructParsedPath();
97
+ this.path = path;
98
+ this.pathType = pathType;
99
+ this.anchorLink = anchorLink;
100
+ this.queryParams = queryParams;
101
+ }
102
+ toString() {
103
+ if (this.pathType === PathType.INTERNAL) {
104
+ const parsedPath = this.path;
105
+ return join(parsedPath.dir, parsedPath.base);
106
+ }
107
+ return this.path.toString();
108
+ }
109
+ getResolvedFiles(allFiles, baseDir) {
110
+ if (this.pathType !== PathType.INTERNAL)
111
+ return [];
112
+ const parsedPath = this.path;
113
+ const resolvedFiles = [];
114
+ if (existsSync(resolve(baseDir, this.toString()))) {
115
+ resolvedFiles.push(join(parsedPath.dir, parsedPath.base));
116
+ }
117
+ // there is at most one file corresponding to the node
118
+ if (parsedPath.ext) {
119
+ return resolvedFiles;
120
+ }
121
+ // there may be multiple files/folders corresponding to the node
122
+ for (const file of allFiles) {
123
+ const fileWithoutLeadingSlash = file.startsWith('/') ? file.slice(1) : file;
124
+ const parsedFilePath = parse(fileWithoutLeadingSlash);
125
+ if (parsedFilePath.dir === parsedPath.dir && parsedFilePath.name === parsedPath.name) {
126
+ resolvedFiles.push(fileWithoutLeadingSlash);
127
+ }
128
+ }
129
+ return resolvedFiles;
130
+ }
131
+ resolvesTo(node, allFiles, baseDir) {
132
+ if (this.pathType !== PathType.INTERNAL)
133
+ return false;
134
+ if (this.toString() === node.toString())
135
+ return true;
136
+ if (this.getResolvedFiles(allFiles, baseDir).includes(node.toString()))
137
+ return true;
138
+ return false;
139
+ }
140
+ static filterMapInternalPaths(paths) {
141
+ return paths
142
+ .map((path) => new MdxPath(path))
143
+ .filter(({ pathType }) => pathType === PathType.INTERNAL)
144
+ .map((path) => path.toString());
145
+ }
146
+ }
147
+ /*
148
+ * A node is a representation of a page
149
+ * One node can contain multiple MdxPaths (corresponding to internal/external links in the page)
150
+ */
151
+ export class Node {
152
+ label;
153
+ paths;
154
+ relativeDir;
155
+ filename;
156
+ edges = [];
157
+ constructor(label, paths = []) {
158
+ this.label = label;
159
+ this.paths = paths;
160
+ this.relativeDir = normalizeDir(dirname(label));
161
+ this.filename = basename(label);
162
+ this.label = join(this.relativeDir, this.filename);
163
+ }
164
+ toString() {
165
+ return this.label;
166
+ }
167
+ equals(other) {
168
+ return this.label === other.label;
169
+ }
170
+ addPath(originalPath, wrapper) {
171
+ const path = new MdxPath(originalPath, this.relativeDir, this.filename, wrapper);
172
+ this.paths.push(path);
173
+ }
174
+ addOutgoingEdge(edge) {
175
+ const existingEdge = this.edges.find((e) => e.equals(edge));
176
+ if (existingEdge) {
177
+ existingEdge.incrementCount();
178
+ }
179
+ else {
180
+ this.edges.push(edge);
181
+ }
182
+ }
183
+ getChildNodes(depth = 1) {
184
+ const children = [];
185
+ for (const edge of this.edges) {
186
+ const child = edge.target;
187
+ children.push(child);
188
+ if (depth > 1) {
189
+ children.push(...child.getChildNodes(depth - 1));
190
+ }
191
+ }
192
+ return children;
193
+ }
194
+ }
195
+ export class Edge {
196
+ source;
197
+ target;
198
+ edgeType;
199
+ count = 0;
200
+ constructor(source, target, edgeType) {
201
+ this.source = source;
202
+ this.target = target;
203
+ this.edgeType = edgeType;
204
+ this.count++;
205
+ }
206
+ incrementCount() {
207
+ this.count++;
208
+ }
209
+ getCount() {
210
+ return this.count;
211
+ }
212
+ equals(other) {
213
+ // directionality of edge matters
214
+ return (this.source.equals(other.source) &&
215
+ this.target.equals(other.target) &&
216
+ this.edgeType === other.edgeType);
217
+ }
218
+ }
219
+ export class Graph {
220
+ baseDir;
221
+ nodes = {};
222
+ edges = [];
223
+ constructor(baseDir) {
224
+ this.baseDir = baseDir;
225
+ this.baseDir = resolve(baseDir);
226
+ }
227
+ addNode(label) {
228
+ if (this.nodes[label])
229
+ return this.nodes[label];
230
+ this.nodes[label] = new Node(label);
231
+ return this.nodes[label];
232
+ }
233
+ addNodes(labels) {
234
+ for (const label of labels) {
235
+ this.addNode(label);
236
+ }
237
+ }
238
+ /*
239
+ * Aliases are additional paths that are valid due to redirects
240
+ */
241
+ addAliasNodes() {
242
+ // TODO move aliases computation to mint config package, since it'll depend on version
243
+ // for now this is a hacky implementation
244
+ const aliases = MdxPath.filterMapInternalPaths(getFileListWithDirectories(this.baseDir));
245
+ this.addNodes(aliases);
246
+ }
247
+ getNode(label) {
248
+ return this.nodes[label];
249
+ }
250
+ addEdge(source, target, edgeType = EdgeType.CONTENT) {
251
+ const newEdge = new Edge(source, target, edgeType);
252
+ const existingEdge = this.edges.find((edge) => {
253
+ if (edge.equals(newEdge)) {
254
+ edge.incrementCount();
255
+ return true;
256
+ }
257
+ return false;
258
+ });
259
+ if (existingEdge)
260
+ return existingEdge;
261
+ this.edges.push(newEdge);
262
+ return newEdge;
263
+ }
264
+ addEdgesBetweenNodes() {
265
+ const allFiles = getLinkPaths(this.baseDir);
266
+ for (const node of Object.values(this.nodes)) {
267
+ for (const path of node.paths) {
268
+ if (path.pathType === PathType.INTERNAL) {
269
+ const targetNode = Object.values(this.nodes).find((otherNode) => path.resolvesTo(otherNode, allFiles, this.baseDir));
270
+ if (targetNode) {
271
+ const edge = this.addEdge(node, targetNode);
272
+ node.addOutgoingEdge(edge);
273
+ }
274
+ else {
275
+ }
276
+ }
277
+ }
278
+ }
279
+ }
280
+ getBrokenInternalLinks() {
281
+ const brokenLinks = [];
282
+ const allFiles = getLinkPaths(this.baseDir);
283
+ for (const node of Object.values(this.nodes)) {
284
+ for (const path of node.paths) {
285
+ if (path.pathType === PathType.INTERNAL) {
286
+ const targetNode = Object.values(this.nodes).find((otherNode) => path.resolvesTo(otherNode, allFiles, this.baseDir));
287
+ if (!targetNode) {
288
+ brokenLinks.push(path);
289
+ }
290
+ }
291
+ }
292
+ }
293
+ return brokenLinks;
294
+ }
295
+ // DEBUGGING
296
+ getAllInternalPaths() {
297
+ return Object.values(this.nodes)
298
+ .flatMap((node) => node.paths)
299
+ .filter((path) => path.pathType === PathType.INTERNAL)
300
+ .map((path) => path.originalPath + ' => ' + path.toString());
301
+ }
302
+ }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,2 @@
1
- export { getUsedInternalLinksInPage, getUsedInternalLinksInSite, } from "./static-checking/getUsedInternalLinks";
2
- export { getValidInternalLinks } from "./static-checking/getValidInternalLinks";
3
- export { getBrokenInternalLinks } from "./static-checking/getBrokenInternalLinks";
4
- export { renameFileAndUpdateLinksInContent } from "./link-renaming/renameFileAndUpdateLinksInContent.js";
1
+ export { getBrokenInternalLinks } from './static-checking/getBrokenInternalLinks.js';
2
+ export { renameFilesAndUpdateLinksInContent } from './link-renaming/renameFileAndUpdateLinksInContent.js';
package/dist/index.js CHANGED
@@ -1,4 +1,2 @@
1
- export { getUsedInternalLinksInPage, getUsedInternalLinksInSite, } from "./static-checking/getUsedInternalLinks";
2
- export { getValidInternalLinks } from "./static-checking/getValidInternalLinks";
3
- export { getBrokenInternalLinks } from "./static-checking/getBrokenInternalLinks";
4
- export { renameFileAndUpdateLinksInContent } from "./link-renaming/renameFileAndUpdateLinksInContent.js";
1
+ export { getBrokenInternalLinks } from './static-checking/getBrokenInternalLinks.js';
2
+ export { renameFilesAndUpdateLinksInContent } from './link-renaming/renameFileAndUpdateLinksInContent.js';
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Renames a link in the file system. If the link is a directory, all links within the directory will be renamed as well.
3
- * @param existingDir - The existing directory or file to rename
4
- * @param newDirName - The new directory or file name
3
+ * @param oldFilePath - The existing directory or file to rename
4
+ * @param newFilePath - The new directory or file name
5
5
  * @param force
6
6
  */
7
- export declare const renameFileAndUpdateLinksInContent: (existingDir: string, newDirName: string, force?: boolean) => Promise<void>;
7
+ export declare const renameFilesAndUpdateLinksInContent: (oldFilePathString: string, newFilePathString: string, force?: boolean) => Promise<void>;
@@ -1,47 +1,51 @@
1
- import { existsSync, lstatSync, renameSync } from "fs";
2
- import { normalize, parse } from "path";
3
- import renameInternalLinksInPage from "./renameInternalLinksInPage.js";
4
- import { isValidPage, removeFileExtension, getPagePaths } from "../prebuild.js";
1
+ import { existsSync, renameSync } from 'fs';
2
+ import path from 'path';
3
+ import { removeFileExtension, getPagePaths, addLeadingSlash } from '../prebuild.js';
4
+ import renameInternalLinksInPage from './renameInternalLinksInPage.js';
5
5
  /**
6
6
  * Renames a link in the file system. If the link is a directory, all links within the directory will be renamed as well.
7
- * @param existingDir - The existing directory or file to rename
8
- * @param newDirName - The new directory or file name
7
+ * @param oldFilePath - The existing directory or file to rename
8
+ * @param newFilePath - The new directory or file name
9
9
  * @param force
10
10
  */
11
- export const renameFileAndUpdateLinksInContent = async (existingDir, newDirName, force = false) => {
12
- existingDir = normalize(existingDir);
13
- newDirName = normalize(newDirName);
14
- const existingDirParsed = parse(existingDir);
15
- const newDirParsed = parse(newDirName);
16
- if (!existsSync(existingDir)) {
17
- throw new Error("File or folder does not exist at " + existingDir);
11
+ export const renameFilesAndUpdateLinksInContent = async (oldFilePathString, newFilePathString, force = false) => {
12
+ const oldFilePath = path.parse(path.normalize(oldFilePathString));
13
+ const newFilePath = path.parse(path.normalize(newFilePathString));
14
+ if (oldFilePath.dir === newFilePath.dir && oldFilePath.base === newFilePath.base) {
15
+ throw new Error('The old and new file paths are the same.');
18
16
  }
19
- if (!force && existsSync(newDirName)) {
20
- throw new Error("File or folder exists at " + newDirName + ". Use --force to overwrite.");
21
- }
22
- const isDirectory = lstatSync(existingDir).isDirectory();
23
- if (!isDirectory) {
24
- if (!isValidPage(existingDirParsed)) {
25
- throw new Error("File to rename must be an MDX or Markdown file.");
17
+ else if (!force && oldFilePath.ext !== newFilePath.ext) {
18
+ if (force) {
19
+ console.log('Warning: the file extensions are not the same.');
20
+ }
21
+ else {
22
+ throw new Error('The file extensions are not the same. Use the --force flag to override.');
26
23
  }
27
- if (!isValidPage(newDirParsed)) {
28
- throw new Error("New file name must be an MDX or Markdown file.");
24
+ }
25
+ // if the file or directory hasn't already been manually renamed, rename it
26
+ if (existsSync(oldFilePathString)) {
27
+ if (!force && existsSync(newFilePathString)) {
28
+ throw new Error('The new file path already exists. Use the --force flag to overwrite it.');
29
29
  }
30
+ renameSync(oldFilePathString, newFilePathString);
31
+ console.log(`Renamed ${oldFilePathString} to ${newFilePathString}.`);
30
32
  }
31
- renameSync(existingDir, newDirName);
32
- console.log(`Renamed ${existingDir} to ${newDirName}.`);
33
- const existingLink = removeFileExtension(existingDirParsed);
34
- const newLink = removeFileExtension(newDirParsed);
33
+ else if (!force) {
34
+ throw new Error('The old file path does not exist. Use the --force flag to skip the file system renaming.');
35
+ }
36
+ const oldLink = addLeadingSlash(removeFileExtension(oldFilePath));
37
+ const newLink = addLeadingSlash(removeFileExtension(newFilePath));
35
38
  const pagesInDirectory = getPagePaths(process.cwd());
36
39
  const renameLinkPromises = [];
37
- pagesInDirectory.forEach((filePath) => {
40
+ for await (const filePath of pagesInDirectory) {
38
41
  renameLinkPromises.push((async () => {
39
- const numRenamedLinks = await renameInternalLinksInPage(filePath, existingLink, newLink);
42
+ const numRenamedLinks = await renameInternalLinksInPage(path.join(process.cwd(), filePath), oldLink, newLink);
40
43
  if (numRenamedLinks > 0) {
41
44
  console.log(`Renamed ${numRenamedLinks} link(s) in ${filePath}`);
42
45
  }
43
46
  })());
44
- });
47
+ }
45
48
  await Promise.all(renameLinkPromises);
46
49
  return;
47
50
  };
51
+ // renameFilesAndUpdateLinksInContent('/test/A.mdx', '/test/hello.mdx');
@@ -1,2 +1,2 @@
1
- declare const renameInternalLinksInPage: (filePath: string, existingLink: string, newLink: string) => Promise<number>;
1
+ declare const renameInternalLinksInPage: (filePath: string, oldLink: string, newLink: string) => Promise<number>;
2
2
  export default renameInternalLinksInPage;
@@ -1,16 +1,15 @@
1
1
  /**
2
2
  * @typedef {import('remark-mdx')}
3
3
  */
4
- import fs from "fs-extra";
5
- import { normalize } from "path";
6
- import { remark } from "remark";
7
- import remarkFrontmatter from "remark-frontmatter";
8
- import remarkGfm from "remark-gfm";
9
- import remarkMdx from "remark-mdx";
10
- import { visit } from "unist-util-visit";
4
+ import fs from 'fs-extra';
5
+ import { normalize } from 'path';
6
+ import { remark } from 'remark';
7
+ import remarkFrontmatter from 'remark-frontmatter';
8
+ import remarkGfm from 'remark-gfm';
9
+ import remarkMdx from 'remark-mdx';
10
+ import { visit } from 'unist-util-visit';
11
11
  /**
12
- * Go through fileContent and replace all links that match existingLink with
13
- * newLink
12
+ * Go through fileContent and replace all links that match existingLink with newLink
14
13
  */
15
14
  const getContentWithRenamedInternalLinks = async (fileContent, existingLink, newLink) => {
16
15
  let numRenamedLinks = 0;
@@ -18,9 +17,7 @@ const getContentWithRenamedInternalLinks = async (fileContent, existingLink, new
18
17
  return (tree) => {
19
18
  visit(tree, (node) => {
20
19
  // ![]() format
21
- if (node.type === "link" &&
22
- node.url &&
23
- normalize(node.url) === existingLink) {
20
+ if (node.type === 'link' && node.url && normalize(node.url) === existingLink) {
24
21
  node.url = newLink;
25
22
  numRenamedLinks++;
26
23
  }
@@ -31,7 +28,7 @@ const getContentWithRenamedInternalLinks = async (fileContent, existingLink, new
31
28
  const file = await remark()
32
29
  .use(remarkMdx)
33
30
  .use(remarkGfm)
34
- .use(remarkFrontmatter, ["yaml", "toml"])
31
+ .use(remarkFrontmatter, ['yaml', 'toml'])
35
32
  .use(remarkMdxReplaceLinks)
36
33
  .process(fileContent);
37
34
  return {
@@ -39,11 +36,11 @@ const getContentWithRenamedInternalLinks = async (fileContent, existingLink, new
39
36
  newContent: String(file),
40
37
  };
41
38
  };
42
- const renameInternalLinksInPage = async (filePath, existingLink, newLink) => {
39
+ const renameInternalLinksInPage = async (filePath, oldLink, newLink) => {
43
40
  const fileContent = fs.readFileSync(filePath).toString();
44
- const { numRenamedLinks, newContent } = await getContentWithRenamedInternalLinks(fileContent, existingLink, newLink);
41
+ const { numRenamedLinks, newContent } = await getContentWithRenamedInternalLinks(fileContent, oldLink, newLink);
45
42
  fs.outputFileSync(filePath, newContent, {
46
- flag: "w",
43
+ flag: 'w',
47
44
  });
48
45
  return numRenamedLinks;
49
46
  };
@@ -1,14 +1,21 @@
1
- import path, { ParsedPath } from "path";
1
+ /// <reference types="node" />
2
+ import path, { ParsedPath } from 'path';
2
3
  export declare const isNotNodeModule: (filePath: ParsedPath) => boolean;
3
4
  export declare const isValidPage: (filePath: ParsedPath) => boolean;
4
5
  export declare const isValidMedia: (filePath: ParsedPath) => boolean;
5
6
  export declare const isValidLink: (filePath: ParsedPath) => boolean;
7
+ export declare const normalizeFilePath: (filePath: string) => path.ParsedPath;
6
8
  export declare const normalizeFilePaths: (fileList: string[]) => path.ParsedPath[];
7
- export declare const filterNodeModules: (fileList: string[]) => path.ParsedPath[];
9
+ export declare const filterNotNodeModules: (fileList: string[]) => path.ParsedPath[];
8
10
  export declare const filterPages: (fileList: string[]) => path.ParsedPath[];
9
11
  export declare const filterMedia: (fileList: string[]) => path.ParsedPath[];
10
12
  export declare const filterLinks: (fileList: string[]) => path.ParsedPath[];
11
13
  export declare const getFullPath: (filePath: ParsedPath) => string;
12
14
  export declare const removeFileExtension: (filePath: ParsedPath) => string;
13
- export declare const removeLeadingSlash: (filePathString: string) => string;
15
+ export declare const addLeadingSlash: (filePathString: string) => string;
16
+ export declare const removeSelectors: (filePathString: string) => string;
14
17
  export declare const getPagePaths: (dirName: string) => string[];
18
+ export declare const getMediaPaths: (dirName: string) => string[];
19
+ export declare const getLinkPaths: (dirName: string) => string[];
20
+ export declare const normalizeDir: (dir: string) => string;
21
+ export declare const removeLeadingSlash: (filePathString: string) => string;