Skip to content

Commit

Permalink
Make it rewrite README.md to Home.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbhmr committed Jul 16, 2023
1 parent da96382 commit 1338297
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
9 changes: 5 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ inputs:
push to the remote wiki. The default is false. This is useful for testing.
required: true
default: false
preprocess-links:
preprocess:
description: >-
Whether or not to preprocess links before pushing to the wiki. This will
not mutate the source tree. It replaces all links to .md files with links
to the corresponding wiki page. The default is true.
If this option is true, we will preprocess the wiki to move the README.md
to Home.md as well as rewriting all .md links to be bare links. This helps
ensure that the Markdown works in source control as well as the wiki. The
default is true.
required: true
default: true
outputs:
Expand Down
66 changes: 46 additions & 20 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,36 @@
// Copyright 2023 Jacob Hummer
// SPDX-License-Identifier: Apache-2.0
import process from "node:process";
import { readFile, writeFile, appendFile, readdir } from "node:fs/promises";
import { copy } from "npm:fs-extra@^11.1.1"
import {
readFile,
writeFile,
appendFile,
readdir,
rename,
} from "node:fs/promises";
import { existsSync } from "node:fs";
import { copy } from "npm:fs-extra@^11.1.1";
import * as core from "npm:@actions/core@^1.10.0";
import { temporaryDirectory } from "npm:tempy@^3.1.0";
import { $ } from "npm:zx@^7.2.2";
import { remark } from "npm:remark@^14.0.3";
import { visit } from "npm:unist-util-visit@^5.0.0";
import { resolve } from "node:path";

console.table(process.env)

const serverURL = core.getInput("github_server_url");
const repo = core.getInput("repository");
const wikiGitURL = `${serverURL}/${repo}.wiki.git`;
$.cwd = temporaryDirectory();
const path = resolve(core.getInput("path"));
process.chdir(temporaryDirectory());
await copy(path, process.cwd());

console.table({ serverURL, repo, wikiGitURL, "$.cwd": $.cwd });
console.table({
serverURL,
repo,
wikiGitURL,
path,
"process.cwd()": process.cwd(),
});

process.env.GH_TOKEN = core.getInput("token");
process.env.GH_HOST = new URL(core.getInput("github_server_url")).host;
Expand All @@ -39,28 +52,41 @@ if (core.getInput("strategy") === "clone") {
await $`git config user.name github-actions[bot]`;
await $`git config user.email 41898282+github-actions[bot]@users.noreply.github.com`;

await appendFile(resolve($.cwd!, ".git/info/exclude"), core.getInput("ignore"));
await copy(core.getInput("path"), $.cwd!);
await appendFile(".git/info/exclude", core.getInput("ignore"));

function plugin() {
function visitor(node: any) {
if (/\.md$/.test(node.url)) {
node.url = node.url.replace(/\.md$/, "");
console.log(`Rewrote to ${node.url}`);
}
if (["true", "1"].includes(core.getInput("preprocess"))) {
// https://github.com/nodejs/node/issues/39960
if (existsSync("README.md")) {
await rename("README.md", "Home.md");
console.log("Moved README.md to Home.md");
}
return (tree: any) => visit(tree, ["link", "linkReference"], visitor);
}

if (["true", "1"].includes(core.getInput("preprocess_links"))) {
const mdRe = /\.(?:md|markdown|mdown|mkdn|mkd|mdwn|mkdown|ron)$/;
const plugin = () => (tree: any) =>
visit(tree, ["link", "linkReference"], (node: any) => {
if (!mdRe.test(node.url)) {
return;
}
if (!new URL(node.url, "file:///-/").href.startsWith("file:///-/")) {
return;
}

const x = node.url;
node.url = node.url.replace(mdRe, "");
if (new URL(node.url, "file:///-/").href === "file:///-/README") {
node.url = "Home";
}

console.log(`Rewrote ${x} to ${node.url}`);
});
for (const file of await readdir($.cwd!)) {
if (!/\.(?:md|markdown|mdown|mkdn|mkd|mdwn|mkdown|ron)$/.test(file)) {
if (!mdRe.test(file)) {
continue;
}

let md = await readFile(resolve($.cwd!, file), "utf-8");
let md = await readFile(file, "utf-8");
md = (await remark().use(plugin).process(md)).toString();
await writeFile(resolve($.cwd!, file), md);
await writeFile(file, md);
}
}

Expand Down

0 comments on commit 1338297

Please sign in to comment.