Skip to content

Commit

Permalink
Post process: small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
joethei authored Feb 22, 2024
1 parent 6283c63 commit 44d52d8
Showing 1 changed file with 116 additions and 97 deletions.
213 changes: 116 additions & 97 deletions config/post-process.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fs = require('fs/promises');
const fs = require('node:fs/promises');
const path = require('path');

// Set the folder path
Expand All @@ -7,124 +7,142 @@ const sourceDir = folderPath;
const targetDir = folderPath;

// Read the folder contents
async function renameFiles(folderPath) {
async function updateFileContent(folderPath) {
try {
const files = await fs.readdir(folderPath, {recursive: true});

for (const file of files) {
// Step 1: Replace escaped backtick with normal backtick
try {
let filePath = path.join(folderPath, file);
const fileContent = await fs.readFile(filePath, 'utf-8');
const newContent = fileContent.replace(/\\`/g, '`');
await fs.writeFile(filePath, newContent, 'utf-8');
console.log(`Replaced all occurrences of escaped backtick with backtick in: ${filePath}`);
} catch (err) {
console.error(`Error processing file: ${err.message}`);
}

// Remove index
if (file === 'index.md') {
await fs.unlink(folderPath + file);
}

// Step 2: Remove all "obsidian." prefixes
if (file.startsWith('obsidian.') && file.endsWith('.md')) {
const newFileName = file.replace('obsidian.', '').replace('Plugin_2', 'Plugin').replace('Plugin\_2', 'Plugin');
const oldFilePath = path.join(folderPath, file);
const newFilePath = path.join(folderPath, newFileName);

// Rename the file
try {
await fs.rename(oldFilePath, newFilePath);
console.log(`Successfully renamed: ${file} to ${newFileName}`);

// Step 2: add alias so links still work
const fileContent = await fs.readFile(newFilePath, 'utf-8');
let newFileContent = `---
aliases: "${newFileName.slice(0, -3)}"
const files = await fs.readdir(folderPath, {recursive: true});

for (const file of files) {

// Remove index
if (file === 'index.md') {
await fs.unlink(folderPath + file);
continue;
}

if(!folderExists(file)) {
continue;
}

const newFileName = file.replace('obsidian.', '').replace('Plugin_2', 'Plugin').replace('Plugin\_2', 'Plugin');

// Step 1: Replace escaped backtick with normal backtick
try {
let filePath = path.join(folderPath, file);
const fileContent = await fs.readFile(filePath, {encoding: 'UTF-8'});
let newFileContent = `---
aliases: "${newFileName.slice(0, -3).replaceAll('\\', '.')}"
cssclasses: hide-title
---
`+ fileContent;

// Somehow we still get Plugin_2 even if `obsidian.d.ts` doesn't contain any reference to it
newFileContent = newFileContent.replaceAll('obsidian.', '').replaceAll('Plugin_2', 'Plugin').replaceAll('Plugin\_2', 'Plugin').replaceAll('Plugin\\_2', 'Plugin');
await fs.writeFile(newFilePath, newFileContent, 'utf-8');
} catch (err) {
console.error(`Error renaming file: ${err.message}`);
}
}
}
newFileContent = newFileContent.replace(/\\`/g, '`');
newFileContent = newFileContent.replaceAll('obsidian.', '');
newFileContent = newFileContent.replaceAll('Plugin_2', 'Plugin')

newFileContent = newFileContent.replaceAll('Plugin\_2', 'Plugin')
newFileContent = newFileContent.replaceAll('Plugin\\_2', 'Plugin');

await fs.writeFile(filePath, newFileContent, {encoding: 'utf-8'});
console.log(`Updated file content: ${filePath}`);
} catch (err) {
console.error(`Error processing file: ${err.message}`);
}
}
} catch (err) {
console.error(`Error reading directory: ${err.message}`);
console.error(`Error reading directory: ${err.message}`);
}
}

async function renameFiles(files) {

try {
const files = await fs.readdir(folderPath, {recursive: true});

for (const file of files) {
if (file.startsWith('obsidian.') && file.endsWith('.md')) {
const newFileName = file.replace('obsidian.', '').replace('Plugin_2', 'Plugin').replace('Plugin\_2', 'Plugin');
const oldFilePath = path.join(folderPath, file);
const newFilePath = path.join(folderPath, newFileName);

// Rename the file
try {
await fs.rename(oldFilePath, newFilePath);
console.log(`Successfully renamed: ${file} to ${newFileName}`);
} catch (err) {
console.error(`Error renaming file: ${err.message}`);
}
}
}
} catch (err) {
console.error(`Error reading directory: ${err.message}`);
}
}

// Step 3: Organize files into folders by separating by '.'
async function createDirectoryIfNotExists(dirPath) {
try {
await fs.access(dirPath);
} catch (err) {
await fs.mkdir(dirPath, { recursive: true });
}
try {
await fs.access(dirPath);
} catch (err) {
await fs.mkdir(dirPath, { recursive: true });
}
}

async function moveFileToHierarchy(filePath) {
const fileName = path.basename(filePath, '.md');
const [category, ...nameParts] = fileName.split('.');
if (nameParts.length === 0) {
return;
}
const name = nameParts.join('.');
const fileName = path.basename(filePath, '.md');
const [category, ...nameParts] = fileName.split('.');
if (nameParts.length === 0) {
return;
}
const name = nameParts.join('.');

const targetCategoryDir = path.join(targetDir, category);
await createDirectoryIfNotExists(targetCategoryDir);
const targetCategoryDir = path.join(targetDir, category);
await createDirectoryIfNotExists(targetCategoryDir);

const targetPath = path.join(targetCategoryDir, `${name}.md`);
await fs.rename(filePath, targetPath);
const targetPath = path.join(targetCategoryDir, `${name}.md`);
await fs.rename(filePath, targetPath);
}

async function folderExists(folderPath) {
try {
const stats = await fs.stat(folderPath);
return stats.isDirectory();
} catch (error) {
if (error.code === 'ENOENT') {
return false;
}
throw error;
}
try {
const stats = await fs.stat(folderPath);
return stats.isDirectory();
} catch (error) {
if (error.code === 'ENOENT') {
return false;
}
throw error;
}
}

async function organizeIntoFolders() {
await createDirectoryIfNotExists(targetDir);
await createDirectoryIfNotExists(targetDir);

try {
const files = await fs.readdir(sourceDir);

for (const file of files) {
if (file.endsWith('.md')) {
const filePath = path.join(sourceDir, file);
await moveFileToHierarchy(filePath);
}
}

const remainingFiles = await fs.readdir(sourceDir);
for (const file of remainingFiles) {
const fileName = path.basename(file, '.md');

if (await folderExists(path.join(sourceDir, fileName))) {
const filePath = path.join(sourceDir, file);
const targetPath = path.join(sourceDir, fileName, file)
await fs.rename(filePath, targetPath);
console.log('Moved ', filePath, ' to ', targetPath);
}
}
try {
const files = await fs.readdir(sourceDir);

} catch (err) {
console.error(`Error reading directory: ${err.message}`);
}
for (const file of files) {
if (file.endsWith('.md')) {
const filePath = path.join(sourceDir, file);
await moveFileToHierarchy(filePath);
}
}

const remainingFiles = await fs.readdir(sourceDir);
for (const file of remainingFiles) {
const fileName = path.basename(file, '.md');

if (await folderExists(path.join(sourceDir, fileName))) {
const filePath = path.join(sourceDir, file);
const targetPath = path.join(sourceDir, fileName, file)
await fs.rename(filePath, targetPath);
console.log('Moved ', filePath, ' to ', targetPath);
}
}

} catch (err) {
console.error(`Error reading directory: ${err.message}`);
}
}

async function updateFileLinks(folderPath) {
Expand Down Expand Up @@ -174,9 +192,10 @@ async function updateFileLinks(folderPath) {
}

async function main() {
await renameFiles(folderPath);
await organizeIntoFolders();
await updateFileLinks(folderPath);
await updateFileContent(folderPath);
await renameFiles(folderPath);
await organizeIntoFolders();
await updateFileLinks(folderPath);
}

main();

0 comments on commit 44d52d8

Please sign in to comment.