fix: generic page import hierarchy (#1747)

* fix page hierarchy

* fix
This commit is contained in:
Philip Okugbe
2025-11-29 11:50:02 +00:00
committed by GitHub
parent 60a8ed6826
commit 520c07a0bc
@@ -175,6 +175,64 @@ export class FileImportTaskService {
}); });
} }
// Create placeholder pages for folders without corresponding files
const foldersWithContent = new Set<string>();
pagesMap.forEach((page) => {
const segments = page.filePath.split('/');
segments.pop(); // remove filename
// Build up all folder paths and mark them as having content
let currentPath = '';
for (const segment of segments) {
currentPath = currentPath ? `${currentPath}/${segment}` : segment;
foldersWithContent.add(currentPath); // All ancestor folders have content
}
});
// Determine if there's a single root container folder
const rootLevelItems = new Set<string>();
pagesMap.forEach((page) => {
const firstSegment = page.filePath.split('/')[0];
rootLevelItems.add(firstSegment);
});
// If all files are in a single root folder and no files at root level exist
let skipRootFolder: string | null = null;
if (rootLevelItems.size === 1) {
const onlyRootItem = Array.from(rootLevelItems)[0];
// Check if this is a folder (not a file at root)
const hasRootFiles = Array.from(pagesMap.keys()).some(
(filePath) => !filePath.includes('/'),
);
if (!hasRootFiles) {
skipRootFolder = onlyRootItem;
}
}
// For each folder with content, create a placeholder page if no corresponding .md or .html exists
foldersWithContent.forEach((folderPath) => {
if (folderPath.toLowerCase() === skipRootFolder.toLowerCase()) {
return;
}
const mdPath = `${folderPath}.md`;
const htmlPath = `${folderPath}.html`;
if (!pagesMap.has(mdPath) && !pagesMap.has(htmlPath)) {
const folderName = path.basename(folderPath);
pagesMap.set(mdPath, {
id: v7(),
slugId: generateSlugId(),
name: stripNotionID(folderName),
content: '',
parentPageId: null,
fileExtension: '.md',
filePath: mdPath,
});
}
});
// parent/child linking // parent/child linking
pagesMap.forEach((page, filePath) => { pagesMap.forEach((page, filePath) => {
const segments = filePath.split('/'); const segments = filePath.split('/');
@@ -316,11 +374,24 @@ export class FileImportTaskService {
for (const [filePath, page] of levelPages) { for (const [filePath, page] of levelPages) {
const absPath = path.join(extractDir, filePath); const absPath = path.join(extractDir, filePath);
let content = await fs.readFile(absPath, 'utf-8'); let content = '';
// Check if file exists (placeholder pages won't have physical files)
try {
await fs.access(absPath);
content = await fs.readFile(absPath, 'utf-8');
if (page.fileExtension.toLowerCase() === '.md') { if (page.fileExtension.toLowerCase() === '.md') {
content = await markdownToHtml(content); content = await markdownToHtml(content);
} }
} catch (err: any) {
if (err?.code === 'ENOENT') {
// Use empty content, title will be the folder name
content = '';
} else {
throw err;
}
}
const htmlContent = const htmlContent =
await this.importAttachmentService.processAttachments({ await this.importAttachmentService.processAttachments({