Skip to content

Commit

Permalink
image update, useage file
Browse files Browse the repository at this point in the history
  • Loading branch information
chdeskur committed May 27, 2024
1 parent af45aad commit 5916889
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
23 changes: 14 additions & 9 deletions clis/readme-migrator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import * as readline from 'readline';
const urlPattern = /<[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&//=]*)>/g;
const imagePattern = /https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9]{1,6}\b(?:[-a-zA-Z0-9@:%_\+.~#?&\/=]*)/g;

function downloadImage(line: string): string {
function downloadImage(line: string, levelIn: number): string {
let pathExtend = ''
for (let i = 0; i < levelIn; i++) {
pathExtend += '../'
}

if (!fs.existsSync('images')) {
fs.mkdirSync('images');
}
Expand All @@ -19,12 +24,12 @@ function downloadImage(line: string): string {
res.pipe(file);
});
}
return line.replace('https://files.readme.io', 'images');
return line.replace('https://files.readme.io', `${pathExtend}../assets/images`);
}
return line;
}

function convertToMarkdown(content: string[]): string {
function convertToMarkdown(content: string[], levelIn: number): string {
let markdown = '';
let collectingImage = false;
let collectingCode = false;
Expand Down Expand Up @@ -54,7 +59,7 @@ function convertToMarkdown(content: string[]): string {
if (matches) {
newline = `src='${matches[0]}' `;
if (newline.includes('files.readme.io')) {
newline = downloadImage(newline);
newline = downloadImage(newline, levelIn);
}
markdown += newline;
} else if (line.includes('caption')) {
Expand All @@ -66,7 +71,7 @@ function convertToMarkdown(content: string[]): string {
}

if (line.includes('files.readme.io')) {
newline = downloadImage(line);
newline = downloadImage(line, levelIn);
}

if (line.match(/```(\S+)/)) {
Expand Down Expand Up @@ -186,7 +191,7 @@ async function promptUser(folderName: string): Promise<boolean> {
});
}

async function copyAndConvertToMdx(srcFolder: string, dstFolder: string): Promise<void> {
async function copyAndConvertToMdx(srcFolder: string, dstFolder: string, levelIn: number): Promise<void> {
try {
if (!fs.existsSync(dstFolder)) {
fs.mkdirSync(dstFolder);
Expand All @@ -205,13 +210,13 @@ async function copyAndConvertToMdx(srcFolder: string, dstFolder: string): Promis
if (err instanceof Error) message = err.message;
console.error(`An error occurred while reading the file: ${message}`);
}
const markdownContent = convertToMarkdown(content);
const markdownContent = convertToMarkdown(content, levelIn);
saveMarkdownFile(markdownContent, dstPath);
console.log(`Copied '${srcPath}' to '${dstPath}'`);
} else if (fs.lstatSync(srcPath).isDirectory()) {
if (await promptUser(item)) {
const nestedDstFolder = path.join(dstFolder, item);
await copyAndConvertToMdx(srcPath, nestedDstFolder);
await copyAndConvertToMdx(srcPath, nestedDstFolder, levelIn + 1);
}
}
}
Expand Down Expand Up @@ -245,7 +250,7 @@ function askQuestion(query: string): Promise<string> {
const itemPath = path.join(folderPath, item);
if (fs.lstatSync(itemPath).isDirectory()) {
if (await promptUser(item)) {
await copyAndConvertToMdx(itemPath, 'pages');
await copyAndConvertToMdx(itemPath, 'pages', 0);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion clis/readme-migrator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"migrate": "ts-node index.ts"
"migrate": "ts-node index.ts",
"clear": "rm -rf ./pages && rm -rf ./images"
},
"repository": {
"type": "git",
Expand Down
24 changes: 24 additions & 0 deletions clis/readme-migrator/usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# How to use ReadMe to Fern Migrator

To run the ReadMe migrator:
1. Export ReadMe documentation
1. Go to project page
1. Navigate to **Configuration** -> **Project Settings**
1. Scroll to the **Project Management** section
1. Click the button to **Export Docs**
1. Unzip file
1. Run migrator using `npm run migrate`
1. When prompted, enter the path to the unzipped docs file
1. The migrator will then prompt you to specify which subfolders you would like to migrate to Fern docs.

Successfully converted files will be stored in a *pages* folder, and any corresponding images will be stored in an *images* folder.

To integrate the pages into a Fern-generated site:
1. Follow the [directions](https://buildwithfern.com/learn/docs/getting-started/quickstart) for setting up documentation
1. Copy the contents of the *pages* folder generated by the migrator into the *fern/docs/pages* folder in your fern docs
1. Move the **entire** *images* folder into the *fern/docs/assets* folder in your fern docs

Remaining set up steps (to be automated in a later update):
- The site navigation must be set up in the `docs.yml` file (this includes fixing url slugs)
- Links which reference the internal documentation must be fixed
- Some custom elements (e.g. `[block:html]`), if present in the docs, will need to be converted manually to embedded HTML

0 comments on commit 5916889

Please sign in to comment.