Skip to content

Commit

Permalink
Merge pull request #30 from kmlbgn/master
Browse files Browse the repository at this point in the history
fix: codebase refactoring + resolved some TODO
  • Loading branch information
kmlbgn authored Feb 28, 2024
2 parents 69773c7 + a2e0bcc commit 2909f7d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 120 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ Docu-notion automatically identifies and removes blocks that are either child pa

# **Custom Pages**

Docusaurus automatically generates custom pages from the `src/pages` directory, creating corresponding slugs and links. Pages located at the root but outside the 'Outline' are treated as custom pages, converted to markdown, and moved to `src/pages`. This setup supports both standard pages and links to database pages.

**Note on Conflicts**: If the 'Outline' contains content, an `index.md` is generated. However, if there's also an `index.js` in `src/pages`, Docusaurus prioritizes the last processed page. Testing indicates that `src/pages` takes precedence over pages in the `docs` folder, therefore `index.md` will not be taken into account.
Docusaurus automatically generates custom pages from the `src/pages` directory, creating corresponding slugs and links. You can create any page within the root page in Notion, for instance naming it "src/page", and manually move these pages into the Docusaurus `src/pages` folder as needed. This approach is simpler and easily managed using github workflow.

**Note on Conflicts**: Pages within `src/pages` are prioritized by Docusaurus and can lead to conflicts with pages that have matching slugs elsewhere in the project. E.g. If both an index.md or a page with "/" slug in the main documentation and an "index.js" in `src/pages` exist, Docusaurus will prioritize the content in `src/pages`, potentially overlooking the index.md.

# Custom parsing (Plugins)

Expand Down
9 changes: 1 addition & 8 deletions src/HierarchicalNamedLayoutStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,12 @@ export class HierarchicalNamedLayoutStrategy extends LayoutStrategy {
.replaceAll("?", "-");

let path;
if (page.subtype === PageSubType.Custom) {
// For Custom pages, store them directly in src/pages
path = this.rootDirectory +`/tmp/${sanitizedName}${extensionWithDot}`;
} else {

// For all other pages, use the existing structure for Docusaurus to parse
const context = ("/" + page.layoutContext + "/").replaceAll("//", "/");
path = this.rootDirectory + context + sanitizedName + extensionWithDot;
return path;
}

return path;
}

//{
// "position": 2.5,
// "label": "Tutorial",
Expand Down
7 changes: 2 additions & 5 deletions src/NotionPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export enum PageType {
}
export enum PageSubType {
CategoryIndex,
Custom,
Content,
}

Expand Down Expand Up @@ -78,11 +77,9 @@ export class NotionPage {
}

public get subtype(): PageSubType {
// Check subtype flag under parent for level pages with index content or custom pages
// Check subtype flag under parent for level pages with index content
let subtype = (this.metadata as any).parent?.subtype;
if (subtype === 'custom') {
return PageSubType.Custom;
} else if (subtype === 'categoryindex') {
if (subtype === 'categoryindex') {
return PageSubType.CategoryIndex;
} else {
return PageSubType.Content;
Expand Down
91 changes: 14 additions & 77 deletions src/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@ export async function notionPull(options: DocuNotionOptions): Promise<void> {
exit(1);
}

// Create a base folder using markdownOutputPath (default "tabs")
await fs.mkdir(options.markdownOutputPath.replace(/\/+$/, ""), { recursive: true });
await getTabs(options, "", "root", options.rootPage);
}


async function getTabs(
options: DocuNotionOptions,
incomingContext: string,
Expand All @@ -114,9 +115,6 @@ async function getTabs(
const r = await getBlockChildren(rootPage.pageId);
const pageInfo = await rootPage.getContentInfo(r);

// Create a 'tmp' folder for pages within "Custom" tab
await fs.mkdir(options.markdownOutputPath.replace(/\/+$/, "") + '/tmp', { recursive: true });

warning(`Scan: Root page is "${rootPage.nameOrTitle}". Scanning for tabs...`);

// Recursively process each tabs
Expand Down Expand Up @@ -160,6 +158,7 @@ async function getTabs(
group("Stage 3: clean up old files & images...");
// TODO: pageWasSeen func is LayoutStrategy is scanning entire root and deleting anything not seen (not part of the pages array)
// It needs to be edited to only scan the tabs path or completely deleted, otherwise it delete all previously parsed tabs.
// Maybe same for OldImages.
// await layoutStrategy.cleanupOldFiles();
// await cleanupOldImages();
endGroup();
Expand All @@ -179,13 +178,9 @@ async function getTabs(
// );
}

//TODO: change description
// This walks the "Outline" page and creates a list of all the nodes that will
// be in the sidebar, including the directories, the pages that are linked to
// that are parented in from the "Database", and any pages we find in the
// outline that contain content (which we call "Simple" pages). Later, we can
// then step through this list creating the files we need, and, crucially, be
// able to figure out what the url will be for any links between content pages.
// getPagesRecursively navigates the root page and iterates over each page within it,
// treating each as an independent tree structure. It constructs a folder structure of pages for sidebar organization,
// preserving the hierarchical order set in Notion.
async function getPagesRecursively(
options: DocuNotionOptions,
incomingContext: string,
Expand All @@ -209,54 +204,7 @@ async function getPagesRecursively(
const r = await getBlockChildren(currentPage.pageId);
const pageInfo = await currentPage.getContentInfo(r);

// // case: root page
// if (
// currentPage.pageId == parentId
// ){
// warning(`Scan: Root page is "${currentPage.nameOrTitle}". Scanning...`);
// let layoutContext = incomingContext;

// // Recursively process each child page...
// for (const childPageInfo of pageInfo.childPageIdsAndOrder) {
// await getPagesRecursively(
// options,
// layoutContext,
// currentPage.pageId,
// childPageInfo.id,
// childPageInfo.order,
// false
// );
// }
// // ... and links to page.
// for (const linkPageInfo of pageInfo.linksPageIdsAndOrder) {
// pages.push(
// await fromPageId(
// options,
// layoutContext,
// currentPage.pageId,
// linkPageInfo.id,
// linkPageInfo.order,
// false,
// true
// )
// );
// }
// }

// // case: custom page contained in the root page to be moved into Docusaurus src/pages folder, except the Outline.
// else if (
// currentPage.nameOrTitle != "Outline" &&
// currentPage.parentId == options.rootPage &&
// currentPage.pageId != options.rootPage
// // pageInfo.hasContent
// ){
// warning(`Scan: Page "${currentPage.nameOrTitle}" is outside the Outline, it will be stored in "src/pages" to be used as your convenience.`);
// // Set subtype flag
// (currentPage.metadata as any).parent.subtype = "custom";
// pages.push(currentPage);
// }

// case: Category page with an index, which creates a dropdown with content in the sidebar
// Case: Category page with an index, which creates a dropdown with content in the sidebar
if (
pageInfo.hasContent &&
(pageInfo.childPageIdsAndOrder.length || pageInfo.linksPageIdsAndOrder.length)
Expand Down Expand Up @@ -302,7 +250,7 @@ async function getPagesRecursively(
}
}

// case: A category page without index which creates a dropdown without content in the sidebar
// Case: A category page without index which creates a dropdown without content in the sidebar
else if (!pageInfo.hasContent &&
(pageInfo.childPageIdsAndOrder.length || pageInfo.linksPageIdsAndOrder.length)
){
Expand Down Expand Up @@ -339,13 +287,13 @@ async function getPagesRecursively(
}
}

// case: A simple content page
else if (pageInfo.hasContent) {
// Case: A simple content page
else if (pageInfo.hasContent) {
warning(`Scan: Page "${currentPage.nameOrTitle}" is a simple content page.`);
pages.push(currentPage);
}

// case: empty pages and undefined ones
// Case: Empty pages and undefined ones
else {
console.info(
warning(
Expand Down Expand Up @@ -486,20 +434,9 @@ async function fromPageId(
metadata,
foundDirectlyInOutline,
});
//TODO: Revamp this, need special logic for Custom page and better handling of link to page type. Because of this workflow doesnt work.
// if (isLink) {
// if (
// parentId == options.rootPage &&
// pageId != options.rootPage &&
// currentPage.nameOrTitle != "Outline"
// ) {
// (currentPage.metadata as any).parent.subtype = "custom";
// warning(`Scan: Page "${currentPage.nameOrTitle}" is a link outside the Outline, it will be stored in "src/pages" to be used as your convenience.`);
// } else {
// warning(`Scan: Page "${currentPage.nameOrTitle}" is a link to a page.`);
// }
// }

if (isLink) {
warning(`Scan: Page "${currentPage.nameOrTitle}" is a link to a database page.`);
}
//logDebug("notion metadata", JSON.stringify(metadata));
return currentPage
}
Expand Down
54 changes: 28 additions & 26 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,37 +87,39 @@ export async function run(): Promise<void> {
path.join(program.opts().cssOutputDirectory, "docu-notion-styles.css")
);

async function moveTmpContents() {
const destTmpPath = "src/pages";
const srcTmpPath = path.join(options.markdownOutputPath.replace(/\/+$/, "")+ '/tmp');
fs.ensureDirSync(destTmpPath);
// TODO: delete if confirmed useless
// async function moveTmpContents() {
// const destTmpPath = "src/pages";
// const srcTmpPath = path.join(options.markdownOutputPath.replace(/\/+$/, "")+ '/tmp');
// fs.ensureDirSync(destTmpPath);

const tmpFiles = fs.readdirSync(srcTmpPath);
for (const file of tmpFiles) {
const destFilePath = path.join(destTmpPath, file);
const srcFilePath = path.join(srcTmpPath, file);
// const tmpFiles = fs.readdirSync(srcTmpPath);
// for (const file of tmpFiles) {
// const destFilePath = path.join(destTmpPath, file);
// const srcFilePath = path.join(srcTmpPath, file);

if (fs.existsSync(destFilePath)) {
// Prompt user for overwriting
const overwrite = await promptUserForOverwrite(file, options.yes);
if (!overwrite) {
console.log(`Skipping overwrite of '${file}'`);
continue;
} else {
console.log(`Overwriting '${file}'`);
}
}
// if (fs.existsSync(destFilePath)) {
// // Prompt user for overwriting
// const overwrite = await promptUserForOverwrite(file, options.yes);
// if (!overwrite) {
// console.log(`Skipping overwrite of '${file}'`);
// continue;
// } else {
// console.log(`Overwriting '${file}'`);
// }
// }

fs.moveSync(srcFilePath, destFilePath, { overwrite: true });
}
// After moving all files, delete the tmp folder
fs.removeSync(srcTmpPath);
}
// fs.moveSync(srcFilePath, destFilePath, { overwrite: true });
// }
// // After moving all files, delete the tmp folder
// fs.removeSync(srcTmpPath);
// }

// pull and move custom pages
// Pull tabs and their respective page tree structure
await notionPull(program.opts());
await moveTmpContents();
console.log("Pull from Notion successful. Custom pages were moved to src/pages.");
// TODO: delete if confirmed useless
// await moveTmpContents();
console.log("Pull from Notion successful.");

}

Expand Down
2 changes: 1 addition & 1 deletion src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
IDocuNotionContext,
IRegexMarkdownModification,
} from "./plugins/pluginTypes";
import { error, info, logDebug, logDebugFn, verbose, warning } from "./log";
import { info, logDebug, logDebugFn, verbose, warning } from "./log";
import { NotionPage } from "./NotionPage";
import { IDocuNotionConfig } from "./config/configuration";
import { NotionBlock } from "./types";
Expand Down

0 comments on commit 2909f7d

Please sign in to comment.