Skip to content

Commit

Permalink
fix(docs): locallinks (#1458)
Browse files Browse the repository at this point in the history
  • Loading branch information
sstraatemans authored and barthuijgen committed Jan 18, 2024
1 parent 6a2d591 commit 0039da7
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 119 deletions.
2 changes: 2 additions & 0 deletions .changeset/great-radios-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
2 changes: 0 additions & 2 deletions packages/apps/docs/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
remarkAdmonitions,
remarkCheckForCodeTitle,
remarkFigureOutOfParagraph,
remarkFixAbsoluteLinks,
remarkFrontmatterToProps,
remarkHeadersToProps,
remarkPropsToStaticRender,
Expand Down Expand Up @@ -82,7 +81,6 @@ const withMDX = mdx({
remarkSideMenuToProps,
remarkPropsToStaticRender,
remarkAdmonitions,
remarkFixAbsoluteLinks,
remarkYoutube,
remarkTwitter,
remarkCheckForCodeTitle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ With this icon

:::note Help

See the [Chainweaver Troubleshoot](./chainweaver-troubleshooting) page if you
See the [Chainweaver Troubleshoot](./chainweaver-troubleshooting.md) page if you
encountered issues installing the .deb

:::
Expand Down Expand Up @@ -112,7 +112,7 @@ Kadena Chainweaver” icon from the desktop

:::note Help

See the [Chainweaver Troubleshoot ](./chainweaver-troubleshooting)page if you
See the [Chainweaver Troubleshoot ](./chainweaver-troubleshooting.md)page if you
encountered issues installing the .ova

:::
Expand Down
52 changes: 0 additions & 52 deletions packages/apps/docs/src/scripts/detectBrokenLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,50 +77,6 @@ function extractBrokenLinksFromTsFile(filePath: string): string[] {
return broken;
}

function getDisallowedLinksFromMdFile(links: string[]): string[] {
const blackListedUrls = [
'medium.com/kadena-io',
'/pages/docs/',
'pact-language.readthedocs.io', //todo when pact docs are approved
// 'docs.kadena.io', some imported pages still have docs.kadena.io (needs to be fixed in importreadme script)
//'api.chainweb.com', todo when pact docs are approved
// 'kadena-io.github.io' ,todo when pact docs are approved
];
return links.reduce((acc: string[], val) => {
const found = blackListedUrls.filter((url) => val.includes(url));

if (found.length) {
return [...acc, `${val} (BLACKLISTED)`];
}

if (!val.startsWith('http') && val.includes('.html')) {
return [...acc, `${val} (NO RELATIVE .HTML files)`];
}
//@TODO: fix checks on relative links
// if (val.startsWith('#')) {
// return [...acc, `${val} (ONLY RELATIVE DEEPLINKS, WITH PATH)`];
// }

return acc;
}, []);
}

function extractBrokenLinksFromMdFile(filePath: string): string[] {
const fileContent = fs.readFileSync(filePath, 'utf8');
const linkRegex = /\[([^\]]+)\]\(([^)]+)\)/g;
const links: string[] = [];
let match;

while ((match = linkRegex.exec(fileContent))) {
links.push(match[2]);
}

return [
...getBrokenLinks(filePath, links),
...getDisallowedLinksFromMdFile(links),
];
}

const filesWithBrokenLinks: LinksType = {};

function processFiles(directory: string): void {
Expand All @@ -135,14 +91,6 @@ function processFiles(directory: string): void {
const fileExtension = getFileExtension(filePath);
const localFilePath = filePath.replace(__dirname, '');

if (fileExtension === 'md') {
const brokenLinks = extractBrokenLinksFromMdFile(filePath);

if (brokenLinks.length > 0) {
filesWithBrokenLinks[localFilePath] = brokenLinks;
}
}

if (fileExtension === 'tsx') {
const brokenLinks = extractBrokenLinksFromTsFile(filePath);

Expand Down
33 changes: 13 additions & 20 deletions packages/apps/docs/src/scripts/fixLocalLinks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ import { toMarkdown } from 'mdast-util-to-markdown';
import { remark } from 'remark';
import type { Root } from 'remark-gfm';
import { loadConfigPages } from '../movePages/utils/loadConfigPages';
import { crawlPage } from '../utils/crawlPage';
import type { IScriptResult } from './../types';
import { getTypes } from './../utils';
import { getFileNameOfPageFile } from './../utils/getFileNameOfPageFile';
import { splitContentFrontmatter } from './../utils/splitContentFrontmatter';
import { refactorAbsoluteDocsLink } from './refactorAbsoluteDocsLink';
import { getCleanedHash } from './utils/getCleanedHash';
import { getFileFromNameOfUrl } from './utils/getFileFromNameOfUrl';
import { getFileNameOfPageFile } from './utils/getFileNameOfPageFile';
import { getLinkHash } from './utils/getLinkHash';
import { getPageFromPath } from './utils/getPageFromPath';
import { getUrlofImageFile } from './utils/getUrlofImageFile';
import { isLocalImageLink, isLocalPageLink } from './utils/isLocalPageLink';
import { removeFileExtenion } from './utils/removeFileExtenion';
import { splitContentFrontmatter } from './utils/splitContentFrontmatter';

const errors: string[] = [];
const success: string[] = [];
Expand Down Expand Up @@ -157,6 +159,10 @@ const fixLinks = async (
for (let i = 0; i < linkArray.length; i++) {
const link = linkArray[i];

//fix docs.kadena.io links to relative links.
//this way SPA links will still work and we can check relative links for 404's
link.url = refactorAbsoluteDocsLink(link.url);

if (isLocalPageLink(link.url)) {
link.url = getUrlofPageFile(link.url);
link.url = await fixHashLinks(link.url);
Expand All @@ -178,32 +184,19 @@ ${newContent}
);
};

const crawlPage = async (
page: IConfigTreeItem,
parentTree: IConfigTreeItem[],
): Promise<void> => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
await fixLinks(page, parentTree);

if (page.children) {
for (let i = 0; i < page.children.length; i++) {
const child = page.children[i];
await crawlPage(child, [...parentTree, page]);
}
}
};

export const fixLocalLinks = async (): Promise<IScriptResult> => {
errors.length = 0;
success.length = 0;
const pages = loadConfigPages();

for (let i = 0; i < pages.length; i++) {
const page = pages[i];
await crawlPage(page, []);
await crawlPage(page, [], fixLinks);
}

success.push('Locallinks are fixed');
if (!errors.length) {
success.push('Locallinks are fixed');
}
return { errors, success };
};

Expand All @@ -217,7 +210,7 @@ export const fixLocalLinksSingle =
errors.push('page not found in config');
} else {
const parentTree = await getParentTreeFromPage(page);
await crawlPage(page, parentTree);
await crawlPage(page, parentTree, fixLinks);
success.push('Locallinks are fixed');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const refactorAbsoluteDocsLink = (path: string): string => {
const regExp = /^(https?:\/\/docs\.kadena\.io)/;

if (path.match(regExp)) return path.replace(regExp, '') || '/';

return path;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { refactorAbsoluteDocsLink } from '../../refactorAbsoluteDocsLink';

describe('utils refactorAbsoluteDocsLink', () => {
it('should return the normal link if not absolute docs link', async () => {
const start = 'https://he-man.com/masters-of-the-universe';
const expectedResult = start;
expect(refactorAbsoluteDocsLink(start)).toBe(expectedResult);
});
it('should return relative link if it is a docs link', async () => {
let start = 'https://docs.kadena.io/build/start';
let expectedResult = '/build/start';
expect(refactorAbsoluteDocsLink(start)).toBe(expectedResult);

start = 'https://docs.kadena.io/build/start.md';
expectedResult = '/build/start.md';
expect(refactorAbsoluteDocsLink(start)).toBe(expectedResult);

start = 'https://docs.kadena.io/build/start.md';
expectedResult = '/build/start.md';
expect(refactorAbsoluteDocsLink(start)).toBe(expectedResult);
});

it('should return relative link if it is a docs link home', async () => {
let start = 'https://docs.kadena.io';
let expectedResult = '/';
expect(refactorAbsoluteDocsLink(start)).toBe(expectedResult);

start = 'https://docs.kadena.io/';
expectedResult = '/';
expect(refactorAbsoluteDocsLink(start)).toBe(expectedResult);

start = 'http://docs.kadena.io/';
expectedResult = '/';
expect(refactorAbsoluteDocsLink(start)).toBe(expectedResult);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const isLocalPageLink = (url: string): boolean => {
const extension = getFileExtension(url);

return (
!url.startsWith('http') &&
(!url.startsWith('http') || url.startsWith('./')) &&
(extension === 'md' || extension === 'mdx' || extension === 'tsx')
);
};
Expand Down
16 changes: 16 additions & 0 deletions packages/apps/docs/src/scripts/utils/__test__/crawlpage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { loadConfigPages } from '@/scripts/fixLocalLinks/utils/loadConfigPages.mock';
import { crawlPage } from '../crawlPage';

describe('utils crawlPage', () => {
it('should crawl all the pages and call the function every time with correct props', async () => {
const pages = await loadConfigPages();
const mock = vi.fn().mockImplementation(() => {});

for (let i = 0; i < pages.length; i++) {
const page = pages[i];
await crawlPage(page, [], mock);
}

expect(mock).toHaveBeenCalledTimes(15);
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { loadConfigPages } from '@/scripts/fixLocalLinks/utils/loadConfigPages.mock';
import { getFileNameOfPageFile } from '../getFileNameOfPageFile';
import { loadConfigPages } from './../loadConfigPages.mock';

vi.mock('@/scripts/movePages', () => {
return {
Expand Down
17 changes: 17 additions & 0 deletions packages/apps/docs/src/scripts/utils/crawlPage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { IConfigTreeItem } from '@kadena/docs-tools';

export const crawlPage = async (
page: IConfigTreeItem,
parentTree: IConfigTreeItem[],
func: (page: IConfigTreeItem, parentTree: IConfigTreeItem[]) => Promise<void>,
): Promise<void> => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
await func(page, parentTree);

if (page.children) {
for (let i = 0; i < page.children.length; i++) {
const child = page.children[i];
await crawlPage(child, [...parentTree, page], func);
}
}
};
2 changes: 0 additions & 2 deletions packages/tools/docs-tools/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import remarkAdmonitions from './remarkAdmonition';
import remarkCheckForCodeTitle from './remarkCheckForCodeTitle';
import remarkFigureOutOfParagraph from './remarkFigureOutOfParagraph';
import remarkFixAbsoluteLinks from './remarkFixAbsoluteLinks';
import remarkFrontmatterToProps from './remarkFrontmatterToProps';
import remarkHeadersToProps from './remarkHeadersToProps';
import remarkPropsToStaticRender from './remarkPropsToStaticRender';
Expand Down Expand Up @@ -47,7 +46,6 @@ export {
remarkAdmonitions,
remarkCheckForCodeTitle,
remarkFigureOutOfParagraph,
remarkFixAbsoluteLinks,
remarkFrontmatterToProps,
remarkHeadersToProps,
remarkPropsToStaticRender,
Expand Down
39 changes: 0 additions & 39 deletions packages/tools/docs-tools/src/remarkFixAbsoluteLinks.ts

This file was deleted.

0 comments on commit 0039da7

Please sign in to comment.