@vrerv/md-to-notion
Version:
An upload of markdown files to a hierarchy of Notion pages.
119 lines • 5.36 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.printFolderHierarchy = exports.readMarkdownFiles = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const gray_matter_1 = __importDefault(require("gray-matter"));
const martian_1 = require("@tryfabric/martian");
const logging_1 = require("./logging");
const replace_links_1 = require("./replace-links");
const logger = (0, logging_1.makeConsoleLogger)("read-md");
/**
* Read and process Markdown files from a specified directory.
* The function reads all Markdown files in the directory and its subdirectories, following symbolic links,
* extracts their content, and converts it to Notion block format.
*
* @param dirPath - The path to the directory containing the Markdown files.
* @param filter - A function that determines if a path should be processed. node_modules are excluded by default.
* @param replacer
* @returns A hierarchical structure of folders and files that contain Markdown files.
*/
function readMarkdownFiles(dirPath, filter = path => !path.includes("node_modules"), replacer) {
function walk(currentPath) {
const folder = {
name: dirPath === currentPath ? "." : path.basename(currentPath),
files: [],
subfolders: [],
};
const entries = fs.readdirSync(currentPath, { withFileTypes: true });
for (const entry of entries) {
const entryPath = path.join(currentPath, entry.name);
const pathFromRoot = path.relative(dirPath, entryPath);
// Add ./ to the path to match start of the path
if (!filter("./" + pathFromRoot)) {
logger(logging_1.LogLevel.INFO, `Skipping path: ${pathFromRoot}`);
continue;
}
let stats;
try {
// Use fs.statSync to follow symbolic links
stats = fs.statSync(entryPath);
}
catch (err) {
console.error(`Error reading path: ${entryPath}`, err);
continue;
}
if (stats.isDirectory()) {
const subfolder = walk(entryPath);
if (subfolder) {
folder.subfolders.push(subfolder);
}
}
else if (stats.isFile() && entry.name.endsWith(".md")) {
const content = (0, gray_matter_1.default)(fs.readFileSync(entryPath, "utf-8")).content;
const fileNameWithoutExtension = path.basename(entry.name, ".md");
folder.files.push({
fileName: fileNameWithoutExtension,
getContent: (linkMap) => {
const noLinkContent = (0, replace_links_1.removeMarkdownLinks)((0, replace_links_1.replaceInternalMarkdownLinks)(content, linkMap, pathFromRoot, replacer));
return (0, martian_1.markdownToBlocks)(noLinkContent);
},
});
}
}
// Return the folder only if it contains any files or subfolders with Markdown files
return folder.files.length > 0 || folder.subfolders.length > 0
? folder
: null;
}
return walk(dirPath);
}
exports.readMarkdownFiles = readMarkdownFiles;
/**
* Prints the folder hierarchy structure.
*
* @param folder - The root folder to start printing from.
* @param indent - The current level of indentation (used for recursion).
*/
function printFolderHierarchy(folder, indent = "") {
if (!folder)
return; // Exit if the folder is null
// Print the current folder's name
logger(logging_1.LogLevel.INFO, `${indent}${folder.name}/`);
// Print the files in the current folder
for (const file of folder.files) {
logger(logging_1.LogLevel.INFO, `${indent} - ${file.fileName}.md`);
}
// Recursively print each subfolder
for (const subfolder of folder.subfolders) {
printFolderHierarchy(subfolder, indent + " "); // Increase indentation for subfolders
}
}
exports.printFolderHierarchy = printFolderHierarchy;
//# sourceMappingURL=read-md.js.map