Created
August 4, 2023 13:11
-
-
Save fandyaditya/54322abe0777bd8475e50cf2fe7a0e32 to your computer and use it in GitHub Desktop.
Node.js Script to Convert Notion Markdown Metadata into Hugo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const folderPath = 'path_to_folder'; // Replace with the path to your folder containing markdown files | |
fs.readdir(folderPath, (err, files) => { | |
if (err) { | |
console.error('Error reading directory:', err); | |
return; | |
} | |
files.forEach((filename) => { | |
if (filename.endsWith('.md')) { | |
const filePath = path.join(folderPath, filename); | |
fs.readFile(filePath, 'utf8', (err, content) => { | |
if (err) { | |
console.error(`Error reading file ${filename}:`, err); | |
return; | |
} | |
// Check if desired content already exists | |
const placeholderPattern = /---\ndate = '{{THE DATE}}'\ntags = \[\{'THE SUB TAGS', 'ANY OTHER SUBTAGS IF MORE THAN ONE'\}\]\ntitle = '{{THE TITLE}}'\n---/; | |
if (placeholderPattern.test(content)) { | |
console.log(`${filename} already has the desired content. Skipping...`); | |
return; | |
} | |
// Remove unwanted lines | |
// const updatedContent = content.replace(/#(.+?)\n\nDate: .+\nFeatured: .+\nSub Tags: .+\nTags: .+\n/, ''); | |
// Extracting relevant information using regular expressions | |
const title = content.match(/#(.+?)\n/) ? content.match(/#(.+?)\n/)[1].trim() : '' | |
const dateString = content.match(/Date: (.+?)\n/) ? content.match(/Date: (.+?)\n/)[1].trim(): '' | |
const dateObj = new Date(dateString); | |
const year = dateObj.getFullYear(); | |
const month = String(dateObj.getMonth() + 1).padStart(2, '0'); | |
const day = String(dateObj.getDate()).padStart(2, '0'); | |
const date = `${year}-${month}-${day}`; | |
const tags = content.match(/Sub Tags: (.+?)\n/) ? content.match(/Sub Tags: (.+?)\n/)[1].trim().split(',') : []; | |
// Replace link references | |
const modifiedContent = content.replace(/\[(.+?)\]\s*\((.+?)\)/g, (match, title, link) => { | |
const modifiedLink = `{{< ref "${link.replace(/\s/g, '%20')}" >}}`; | |
return `[${title}](${modifiedLink})`; | |
}); | |
const updatedContent = modifiedContent.replace(/# (.*?)\n\nTags: (.*?)\nDate: (.*?)\nSub Tags: (.*?)\n/, ''); | |
// Creating the new content with the desired format | |
const newContent = `---\ndate: '${date}'\ntags: [${tags.map((tag) => `'${tag.trim()}'`).join(', ')}]\ntitle: '${title}'\n---\n\n${updatedContent}`; | |
// Writing the updated content back to the file | |
fs.writeFile(filePath, newContent, 'utf8', (err) => { | |
if (err) { | |
console.error(`Error writing to file ${filename}:`, err); | |
return; | |
} | |
console.log(`Updated ${filename}`); | |
}); | |
}); | |
} | |
}); | |
}); | |
console.log('Markdown files update process started.'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment