From 03c4c0b7052816d89e1461d3a75d0a696aa30277 Mon Sep 17 00:00:00 2001 From: Chris Owen Date: Thu, 13 May 2021 11:30:31 +0100 Subject: [PATCH] Fix Trailing Slash Issue --- docusaurus.config.js | 3 +- package-lock.json | 98 ++++++++++++++++++++++++++++++++++++++++++-- package.json | 2 + sitePlugin.js | 51 +++++++++++++++++++++++ 4 files changed, 149 insertions(+), 5 deletions(-) create mode 100644 sitePlugin.js diff --git a/docusaurus.config.js b/docusaurus.config.js index 374d95fe76..09225e6581 100644 --- a/docusaurus.config.js +++ b/docusaurus.config.js @@ -13,7 +13,7 @@ module.exports = { docs: { sidebarPath: require.resolve("./sidebars.js"), editUrl: "https://github.com/CodeYourFuture/Syllabus-V2/edit/master/", - routeBasePath: "/", // Set this value to '/'. + routeBasePath: "/", }, theme: { customCss: require.resolve("./src/css/custom.css"), @@ -21,6 +21,7 @@ module.exports = { }, ], ], + plugins: ["./sitePlugin"], themeConfig: { googleAnalytics: { trackingID: "UA-159979458-2", diff --git a/package-lock.json b/package-lock.json index 7e4c82269a..b4f8770e46 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1431,6 +1431,17 @@ "schema-utils": "^3.0.0" } }, + "fs-extra": { + "version": "9.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz", + "integrity": "sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==", + "requires": { + "at-least-node": "^1.0.0", + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + } + }, "schema-utils": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-3.0.0.tgz", @@ -1497,6 +1508,17 @@ "schema-utils": "^3.0.0" } }, + "fs-extra": { + "version": "9.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz", + "integrity": "sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==", + "requires": { + "at-least-node": "^1.0.0", + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + } + }, "schema-utils": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-3.0.0.tgz", @@ -1529,6 +1551,19 @@ "remark-admonitions": "^1.2.1", "tslib": "^2.1.0", "webpack": "^5.28.0" + }, + "dependencies": { + "fs-extra": { + "version": "9.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz", + "integrity": "sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==", + "requires": { + "at-least-node": "^1.0.0", + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + } + } } }, "@docusaurus/plugin-content-docs": { @@ -1578,6 +1613,17 @@ "strip-final-newline": "^2.0.0" } }, + "fs-extra": { + "version": "9.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz", + "integrity": "sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==", + "requires": { + "at-least-node": "^1.0.0", + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + } + }, "get-stream": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-6.0.1.tgz", @@ -1683,6 +1729,19 @@ "fs-extra": "^9.1.0", "sitemap": "^6.3.6", "tslib": "^2.1.0" + }, + "dependencies": { + "fs-extra": { + "version": "9.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz", + "integrity": "sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==", + "requires": { + "at-least-node": "^1.0.0", + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + } + } } }, "@docusaurus/preset-classic": { @@ -1739,6 +1798,19 @@ "prop-types": "^15.7.2", "react-router-dom": "^5.2.0", "rtlcss": "^3.1.2" + }, + "dependencies": { + "fs-extra": { + "version": "9.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz", + "integrity": "sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==", + "requires": { + "at-least-node": "^1.0.0", + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + } + } } }, "@docusaurus/theme-common": { @@ -1803,6 +1875,17 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz", "integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==" + }, + "fs-extra": { + "version": "9.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz", + "integrity": "sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==", + "requires": { + "at-least-node": "^1.0.0", + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + } } } }, @@ -5104,11 +5187,10 @@ "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" }, "fs-extra": { - "version": "9.1.0", - "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz", - "integrity": "sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==", + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.0.0.tgz", + "integrity": "sha512-C5owb14u9eJwizKGdchcDUQeFtlSHHthBk8pbX9Vc1PFZrLombudjDnNns88aYslCyF6IY5SUw3Roz6xShcEIQ==", "requires": { - "at-least-node": "^1.0.0", "graceful-fs": "^4.2.0", "jsonfile": "^6.0.1", "universalify": "^2.0.0" @@ -5204,6 +5286,14 @@ "is-glob": "^4.0.1" } }, + "glob-promise": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/glob-promise/-/glob-promise-4.1.0.tgz", + "integrity": "sha512-wOdaX1+QJi3ldbjq4fXX/BbGSjhsG6eGXqMnBjQj9ubDiDLvrXbbXRj02rA0CXbMMM7J58dajiQ72va63D7pNw==", + "requires": { + "@types/glob": "^7.1.3" + } + }, "glob-to-regexp": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/glob-to-regexp/-/glob-to-regexp-0.4.1.tgz", diff --git a/package.json b/package.json index 34d794607a..f0a7e87afd 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,8 @@ "@material-ui/core": "^4.11.0", "@material-ui/icons": "^4.9.1", "classnames": "^2.2.6", + "fs-extra": "^10.0.0", + "glob-promise": "^4.1.0", "react": "^16.8.4", "react-dom": "^16.8.4", "react-vertical-timeline-component": "^3.0.2" diff --git a/sitePlugin.js b/sitePlugin.js new file mode 100644 index 0000000000..ddfb61744d --- /dev/null +++ b/sitePlugin.js @@ -0,0 +1,51 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +const glob = require("glob-promise"); +const path = require("path"); +const fs = require("fs-extra"); + +// quick fix for GH pages trailing slash issues +// /myDoc/index.html => /myDoc.html +async function generateSimpleHtmlFiles(outDir) { + console.log("generateSimpleHtmlFiles", outDir); + + const pattern = path.join(outDir, "/**/index.html"); + // console.log('pattern', pattern); + + const filePaths = (await glob(pattern)).filter((filePath) => { + return filePath !== path.join(outDir, "/index.html"); + }); + + // console.log('filePaths', filePaths); + + await Promise.all( + filePaths.map(async (filePath) => { + if ((await fs.stat(filePath)).isDirectory()) { + return; + } + // console.log(file); + const filePathCopy = `${path.dirname(filePath)}.html`; + if (await fs.pathExists(filePathCopy)) { + // console.log(`Skipping ${filePathCopy}`); + } else { + await fs.copyFile(filePath, filePathCopy); + // console.log(`Created ${filePathCopy}`); + } + }) + ); +} + +module.exports = function () { + console.log("site plugin"); + return { + plugin: "site-plugin", + async postBuild(props) { + await generateSimpleHtmlFiles(props.outDir); + }, + }; +};