Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updates to the link fixers #669

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/publish-site-beta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ jobs:
- name: Fix api broken links
working-directory: ./src
run: |
node ./scripts/api-broken-link-fixer.js
node ./scripts/api-broken-link-fixer.js ./docs/api/

#docusaurus build
- name: Use Node.js
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/publish-site-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ jobs:
- name: Fix api broken links
working-directory: ./src
run: |
node ./scripts/api-broken-link-fixer.js
node ./scripts/api-broken-link-fixer.js ./docs/api/

- name: Fix Meadow.Foundation periphs broken links
working-directory: ./src
run: |
node ./scripts/meadow-foundation-broken-link-fixer.js
# - name: Fix Meadow.Foundation periphs broken links
# working-directory: ./src
# run: |
# node ./scripts/meadow-foundation-broken-link-fixer.js

#docusaurus build
- name: Use Node.js
Expand Down
11 changes: 6 additions & 5 deletions .github/workflows/publish-site-sandbox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,13 @@ jobs:
- name: Fix api broken links
working-directory: ./src
run: |
node ./scripts/api-broken-link-fixer.js
node ./scripts/api-broken-link-fixer.js ./docs/api/

- name: Fix Meadow.Foundation periphs broken links
working-directory: ./src
run: |
node ./scripts/meadow-foundation-broken-link-fixer.js
# - name: Fix Meadow.Foundation periphs broken links
# working-directory: ./src
# run: |
# node ./scripts/meadow-foundation-broken-link-fixer.js &&
# node ./scripts/meadow-foundation-broken-link-fixer.js ./docs/api/Meadow.Foundation.FeatherWings/

#docusaurus build
- name: Use Node.js
Expand Down
46 changes: 39 additions & 7 deletions scripts/api-broken-link-fixer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { Dirent } = require("fs");
const path = require("path");

// Get the starting directory from command line arguments
const startDir = process.argv[2] ? process.argv[2] : "./docs/api/";
const startDir = process.argv[2] ? process.argv[2] : "./api/";

// Function to replace the specified pattern in file content
const replacePatternInFile = async (dir, file) => {
Expand Down Expand Up @@ -40,14 +40,43 @@ const replacePatternInFile = async (dir, file) => {
const pattern2 = /(?:\[(.*)\]\(..\/(.*)\))/g;
const slugPattern = /slug: (.*)/;
const slugMatches = data.match(slugPattern);
const slug = slugMatches ? slugMatches[0] : null;
const slug = slugMatches ? slugMatches[1] : null;
// Replacement logic adjusted for the specific regex
result = data.replace(pattern2, (match, p1, p2) => {
if (!p1.includes(p2)) return match; //We are only interested in the broken ones
let newString = match;
const baseClass = p1.replace(`.${p2}`, "");
if (slug?.includes(baseClass)) return match; //can't go around breaking working links now...
const newString = `[${p1}](../../${baseClass}/${p2})`;
// console.log(newString);
const { slugBaseClass, slugBasePath } = ((ss) => {
let parts = slug.split("/");
const slugBaseClass = parts[parts.length - 2];
let slugBasePath = parts.join("/").replace(parts[parts.length - 1], "");
slugBasePath = slugBasePath.slice(0, -1);
return {
slugBaseClass,
slugBasePath,
};
})();

filePath;
if (slugBaseClass === baseClass) {
newString = `[${p1}](${slugBasePath}/${p2})`;
} else {
if (!p1.includes(p2)) {
const linkBaseClassArr = baseClass.split(".");
linkBaseClassArr.pop();
const linkBaseClass = linkBaseClassArr.join(".");
if (linkBaseClass === slugBaseClass) {
newString = `[${p1}](${slugBasePath}/${p2})`;
} else {
newString = `[${p1}](${slugBasePath}/${linkBaseClass}/${p2})`;
}
} else {
let libParts = baseClass.split(".");
libParts.pop();
let lib = libParts.join(".");
newString = `[${p1}](/docs/api/${lib}/${baseClass}/${p2})`;
}
}

return newString;
});

Expand All @@ -63,7 +92,10 @@ const replacePatternInFile = async (dir, file) => {
let data = await fs.readFile(filePath, "utf8");
const pattern3 = /(?:\[View Source\]\((.* .*)\))/g;
// Replacement logic adjusted for the specific regex
result = data.replace(pattern3, (match, p1) => `[View Source](${p1.replace(" ", "%20")})`);
result = data.replace(
pattern3,
(match, p1) => `[View Source](${p1.replace(" ", "%20")})`
);

await fs.writeFile(filePath, result, "utf8");
console.log(`File updated: ${filePath}`);
Expand Down
2 changes: 1 addition & 1 deletion scripts/meadow-foundation-broken-link-fixer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fs.readdir(directoryPath, (err, files) => {
const baseClass = p1.replace(`.${p2}`, "");
// if(p1.includes('Meadow.Foundation.Sensors.Atmospheric.Ccs811.MeasurementMode'))
// console.log(p1, p2, baseClass);
const newString = `[${p1}](../${baseClass}/${p2})`;
const newString = (p1.includes(p2)) ? `[${p1}](../${baseClass}/${p2})` : `[${p1}](../${baseClass}/)`;
console.log(newString);
return newString;
});
Expand Down
Loading