Skip to content

Commit

Permalink
Update to support license variables
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-solomon committed Dec 16, 2024
1 parent 869b97a commit e52b174
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 16 deletions.
7 changes: 6 additions & 1 deletion docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import type { Config } from "@docusaurus/types";
import { themes as prismThemes } from "prism-react-renderer";
import remarkTokenReplacer from "./src/plugins/replace-tokens";
import latestVersions from "./src/plugins/latest-versions";
import licenses from "./src/plugins/licenses";

const allReplacements = {
...latestVersions,
...licenses,
}
const description = "Large-scale automated source code refactoring"

const structuredData = {
Expand Down Expand Up @@ -74,7 +79,7 @@ const config: Config = {
[
remarkTokenReplacer,
{
replacements: latestVersions,
replacements: allReplacements,
},
],
],
Expand Down
57 changes: 57 additions & 0 deletions src/plugins/licenses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const licenses = {
// Apache
"REWRITE_LICENSE_LINK": "[Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0)",
"REWRITE_OPENAPI_LICENSE_LINK": "[Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0)",
"REWRITE_MICRONAUT_LICENSE_LINK": "[Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0)",
"REWRITE_GRADLE_TOOLING_MODEL_LICENSE_LINK": "[Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0)", // ?
"REWRITE_JAVA_DEPENDENCIES_LICENSE_LINK": "[Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0)",
"REWRITE_ALL_LICENSE_LINK": "[Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0)",
"REWRITE_BUILD_GRADLE_PLUGIN_LICENSE_LINK": "[Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0)", // ?
"REWRITE_LIBERTY_LICENSE_LINK": "[Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0)",
"REWRITE_QUARKUS_LICENSE_LINK": "[Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0)",
"REWRITE_ANALYSIS_LICENSE_LINK": "[Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0)",
"REWRITE_TEMPLATING_LICENSE_LINK": "[Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0)",
"REWRITE_THIRD_PARTY_LICENSE_LINK": "[Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0)",
"REWRITE_KOTLIN_LICENSE_LINK": "[Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0)",

// Moderne Source Available
"REWRITE_GRADLE_PLUGIN_LICENSE_LINK": "[Moderne Source Available License](https://docs.moderne.io/licensing/moderne-source-available-license/)",
"REWRITE_MAVEN_PLUGIN_LICENSE_LINK": "[Moderne Source Available License](https://docs.moderne.io/licensing/moderne-source-available-license/)",
"REWRITE_SPRING_LICENSE_LINK": "[Moderne Source Available License](https://docs.moderne.io/licensing/moderne-source-available-license/)",
"REWRITE_LOGGING_FRAMEWORKS_LICENSE_LINK": "[Moderne Source Available License](https://docs.moderne.io/licensing/moderne-source-available-license/)",
"REWRITE_MIGRATE_JAVA_LICENSE_LINK": "[Moderne Source Available License](https://docs.moderne.io/licensing/moderne-source-available-license/)",
"REWRITE_STATIC_ANALYSIS_LICENSE_LINK": "[Moderne Source Available License](https://docs.moderne.io/licensing/moderne-source-available-license/)",
"REWRITE_STRUTS_LICENSE_LINK": "[Moderne Source Available License](https://docs.moderne.io/licensing/moderne-source-available-license/)",
"REWRITE_TESTING_FRAMEWORKS_LICENSE_LINK": "[Moderne Source Available License](https://docs.moderne.io/licensing/moderne-source-available-license/)",
"REWRITE_OKHTTP_LICENSE_LINK": "[Moderne Source Available License](https://docs.moderne.io/licensing/moderne-source-available-license/)",
"REWRITE_JENKINS_LICENSE_LINK": "[Moderne Source Available License](https://docs.moderne.io/licensing/moderne-source-available-license/)",
"REWRITE_APACHE_LICENSE_LINK": "[Moderne Source Available License](https://docs.moderne.io/licensing/moderne-source-available-license/)",
"REWRITE_MICROMETER_LICENSE_LINK": "[Moderne Source Available License](https://docs.moderne.io/licensing/moderne-source-available-license/)",
"REWRITE_DOCKER_LICENSE_LINK": "[Moderne Source Available License](https://docs.moderne.io/licensing/moderne-source-available-license/)",
"REWRITE_HIBERNATE_LICENSE_LINK": "[Moderne Source Available License](https://docs.moderne.io/licensing/moderne-source-available-license/)",
"REWRITE_CUCUMBER_JVM_LICENSE_LINK": "[Moderne Source Available License](https://docs.moderne.io/licensing/moderne-source-available-license/)",
"REWRITE_FEATURE_FLAGS_LICENSE_LINK": "[Moderne Source Available License](https://docs.moderne.io/licensing/moderne-source-available-license/)",
"REWRITE_GITLAB_LICENSE_LINK": "[Moderne Source Available License](https://docs.moderne.io/licensing/moderne-source-available-license/)",

// Moderne Proprietary License
"REWRITE_LANGUAGE_LICENSE_LINK": "[Moderne Proprietary License](https://docs.moderne.io/licensing/overview/)", // ?
"REWRITE_CODEMODS_NG_LICENSE_LINK": "[Moderne Proprietary License](https://docs.moderne.io/licensing/overview/)",
"REWRITE_SQL_LICENSE_LINK": "[Moderne Proprietary License](https://docs.moderne.io/licensing/overview/)",
"REWRITE_CIRCLECI_LICENSE_LINK": "[Moderne Proprietary License](https://docs.moderne.io/licensing/overview/)",
"REWRITE_JAVA_SECURITY_LICENSE_LINK": "[Moderne Proprietary License](https://docs.moderne.io/licensing/overview/)",
"REWRITE_NODEJS_LICENSE_LINK": "[Moderne Proprietary License](https://docs.moderne.io/licensing/overview/)",
"REWRITE_AI_SEARCH_LICENSE_LINK": "[Moderne Proprietary License](https://docs.moderne.io/licensing/overview/)",
"REWRITE_GITHUB_ACTIONS_LICENSE_LINK": "[Moderne Proprietary License](https://docs.moderne.io/licensing/overview/)",
"REWRITE_POLYGLOT_LICENSE_LINK": "[Moderne Proprietary License](https://docs.moderne.io/licensing/overview/)", // ?
"REWRITE_TERRAFORM_LICENSE_LINK": "[Moderne Proprietary License](https://docs.moderne.io/licensing/overview/)",
"REWRITE_ANDROID_LICENSE_LINK": "[Moderne Proprietary License](https://docs.moderne.io/licensing/overview/)",
"REWRITE_CONCOURSE_LICENSE_LINK": "[Moderne Proprietary License](https://docs.moderne.io/licensing/overview/)",
"REWRITE_REACTIVE_STREAMS_LICENSE_LINK": "[Moderne Proprietary License](https://docs.moderne.io/licensing/overview/)",
"REWRITE_MIGRATE_PYTHON_LICENSE_LINK": "[Moderne Proprietary License](https://docs.moderne.io/licensing/overview/)", // ?
"REWRITE_KUBERNETES_LICENSE_LINK": "[Moderne Proprietary License](https://docs.moderne.io/licensing/overview/)",
"REWRITE_JACKSON_LICENSE_LINK": "[Moderne Proprietary License](https://docs.moderne.io/licensing/overview/)",
"REWRITE_CODEMODS_LICENSE_LINK": "[Moderne Proprietary License](https://docs.moderne.io/licensing/overview/)",
"REWRITE_DOTNET_LICENSE_LINK": "[Moderne Proprietary License](https://docs.moderne.io/licensing/overview/)",
};

export default licenses;
77 changes: 62 additions & 15 deletions src/plugins/replace-tokens.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,79 @@
import parse from "remark-parse";
import { unified } from "unified";
import { visit } from "unist-util-visit";

function remarkTokenReplacer(options) {
const replacements = options.replacements || {};

const replaceTokens = (node) => {
// test to see if any of the replacement tokens are in the node
const hasToken = Object.keys(replacements).some((token) =>
node.value.includes(token)
const replaceTokens = (node, index, parent) => {
// Test to see if any of the replacement tokens are in the node
const hasToken = Object.keys(replacements).some(
(token) => node.value && node.value.includes(token)
);

if (!hasToken) {
return;
} else {
Object.entries(replacements).forEach(([token, value]) => {
if (!node.value.includes(token)) {
return;
if (!hasToken) return

Object.entries(replacements).forEach(([token, value]) => {
if (!node.value.includes(token)) {
return;
}

// If the `token` ends with `_LINK`, then replace this node with a link node
if (token.endsWith("_LINK")) {
// Parse the replacement value, assumed to be markdown link
const parsedMarkdown = unified().use(parse).parse(value);
let replacementLink;

visit(parsedMarkdown, "link", (node) => {
replacementLink = node;
});

// If the entire text node is only the token, replace the entire node
// Else if it is part of a larger text node, replace just the token
if (node.value === token) {
parent.children.splice(index, 1, replacementLink);
} else {
const [before, after] = node.value.split(token);
const beforeNode = { type: "text", value: before };
const afterNode = { type: "text", value: after };
parent.children.splice(
index,
1,
beforeNode,
replacementLink,
afterNode
);
}
const oldValue = String(node.value);
} else {
node.value = node.value.replaceAll(new RegExp(token, "gs"), value);
});
}
}
});
};

// Used for replacing variables inside of links such as:
// https://someplace.com/MY_VAR/something
const replaceLink = (node) => {
// Test to see if any of the replacement tokens are in the node
const hasToken = Object.keys(replacements).some(
(token) => node.url && node.url.includes(token)
);

if (!hasToken) return;

Object.entries(replacements).forEach(([token, value]) => {
if (!node.url.includes(token)) {
return;
}

node.url = node.url.replaceAll(new RegExp(token, "gs"), value);
});
};

return (tree) => {
// print the tree to see what it looks like
visit(tree, "text", replaceTokens);
visit(tree, "code", replaceTokens);
visit(tree, "link", replaceLink);
};
}

export default remarkTokenReplacer;
export default remarkTokenReplacer;

0 comments on commit e52b174

Please sign in to comment.