Skip to content

Commit

Permalink
pathToUrlResolver / utils functions /
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedsalem401 committed Nov 13, 2023
1 parent f7ef9d5 commit 6712e30
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 40 deletions.
52 changes: 34 additions & 18 deletions src/lib/indexFolderToObjects.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,55 @@
import { WikiLink, getUniqueValues, recursiveWalkDir } from "../utils/index.js";
import { markdownToObject } from "./markdownToObject.js";
import { getUniqueValues, recursiveWalkDir } from "../utils/index.js";
import type { WikiLink } from "../utils/index.js";
import { extractFileSchemeFromObject } from "../utils/extractFileSchemeFromObject.js";
import { readLocalMarkdownFileToObject } from "./readLocalMarkdownFileToObject.js";
import { File, FileTag, Link, Tag } from "./types/schemaTypes.js";

Check warning on line 5 in src/lib/indexFolderToObjects.ts

View workflow job for this annotation

GitHub Actions / Lint & format check

'Tag' is defined but never used

export function indexFolderToObjects(
folderPath: string,
ignorePatterns?: RegExp[],
pathToUrlResolver?: (filePath: string) => string
pathToUrlResolver: (filePath: string) => string,
ignorePatterns?: RegExp[]
) {
const filePathsToIndex = recursiveWalkDir(folderPath);
const files: File[] = [];
const tags: Tag[] = [];
const tags: string[] = [];
const fileTags: FileTag[] = [];
const links: Link[] = [];
const filteredFilePathsToIndex = filePathsToIndex.filter((filePath) => {
return !(
ignorePatterns && ignorePatterns.some((pattern) => pattern.test(filePath))
);
});
const filteredFilePathsToIndex = filePathsToIndex.filter((filePath) =>
shouldIncludeFile(filePath, ignorePatterns)
);

for (const filePath of filteredFilePathsToIndex) {
const fileObject = markdownToObject(folderPath, filePath, filePathsToIndex);
const fileObject = readLocalMarkdownFileToObject(
folderPath,
filePath,
filePathsToIndex,
pathToUrlResolver
);

const file = extractFileSchemeFromObject(fileObject);
files.push(file);

files.push(fileObject.file);
tags.push(...fileObject.tags);

const fileTagsToInsert = fileObject.tags.map((tag) => ({
tag: tag.name,
file: fileObject.file._id,
tag: tag,
file: fileObject._id,
}));
const uniqueFileTags = getUniqueValues(fileTagsToInsert);
fileTags.push(...uniqueFileTags);
fileTags.push(...fileTagsToInsert);

const linksToInsert: Link[] = processWikiLinks(
fileObject.links,
fileObject.file._id,
fileObject._id,
filePathsToIndex
);
links.push(...linksToInsert);
}

const uniqueTags = getUniqueValues(tags);
const TagsToInsert = uniqueTags.map((tag) => ({ name: tag }));
return {
files: files,
tags: uniqueTags,
tags: TagsToInsert,
fileTags: fileTags,
links: links,
};
Expand All @@ -61,3 +68,12 @@ function processWikiLinks(
}))
.filter((link) => link.to !== undefined);
}

function shouldIncludeFile(
filePath: string,
ignorePatterns?: RegExp[]
): boolean {
return !(
ignorePatterns && ignorePatterns.some((pattern) => pattern.test(filePath))
);
}
24 changes: 8 additions & 16 deletions src/lib/markdowndb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@ import knex, { Knex } from "knex";

import { MddbFile, MddbTag, MddbFileTag, MddbLink } from "./schema.js";
import { DbQueryManager } from "./DbQueryManager.js";
import { FilesQuery, LinkQuery } from "./types/DbQueryTypes.js";
import type { FilesQuery, LinkQuery } from "./types/DbQueryTypes.js";
import { indexFolderToObjects } from "./indexFolderToObjects.js";

export const defaultFilePathToUrl = (filePath: string) => {
let url = filePath
.replace(/\.(mdx|md)/, "")
.replace(/\\/g, "/") // replace windows backslash with forward slash
.replace(/(\/)?index$/, ""); // remove index from the end of the permalink
url = url.length > 0 ? url : "/"; // for home page
return encodeURI(url);
};
import { defaultFilePathToUrl } from "../utils/defaultFilePathToUrl.js";

export class MarkdownDB {
config: Knex.Config;
Expand Down Expand Up @@ -42,14 +34,14 @@ export class MarkdownDB {
await resetDatabaseTables(this.db);
const { files, tags, fileTags, links } = indexFolderToObjects(
folderPath,
ignorePatterns,
pathToUrlResolver
pathToUrlResolver,
ignorePatterns
);

MddbFile.batchInsert(this.db, files);
MddbTag.batchInsert(this.db, tags);
MddbFileTag.batchInsert(this.db, fileTags);
MddbLink.batchInsert(this.db, links);
await MddbFile.batchInsert(this.db, files);
await MddbTag.batchInsert(this.db, tags);
await MddbFileTag.batchInsert(this.db, fileTags);
await MddbLink.batchInsert(this.db, links);
}

async getFileById(id: string): Promise<MddbFile | null> {
Expand Down
7 changes: 3 additions & 4 deletions src/lib/types/FileObject.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { WikiLink } from "../../utils";
import { File, Tag } from "./schemaTypes";
import { File } from "./schemaTypes";

export interface FileObject {
file: File;
tags: Tag[];
export interface FileObject extends File {
tags: string[];
links: WikiLink[];
}
2 changes: 1 addition & 1 deletion src/lib/types/schemaTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ export enum Table {
Tags = "tags",
FileTags = "file_tags",
Links = "links",
}
}
8 changes: 8 additions & 0 deletions src/utils/defaultFilePathToUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const defaultFilePathToUrl = (filePath: string) => {
let url = filePath
.replace(/\.(mdx|md)/, "")
.replace(/\\/g, "/") // replace windows backslash with forward slash
.replace(/(\/)?index$/, ""); // remove index from the end of the permalink
url = url.length > 0 ? url : "/"; // for home page
return encodeURI(url);
};
12 changes: 12 additions & 0 deletions src/utils/extractFileSchemeFromObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { FileObject } from "../lib/types/FileObject.js";

export function extractFileSchemeFromObject(fileObject: FileObject) {
return {
_id: fileObject._id,
file_path: fileObject.file_path,
extension: fileObject.extension,
url_path: fileObject.url_path,
filetype: fileObject.filetype,
metadata: fileObject.metadata,
};
}
4 changes: 3 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export { recursiveWalkDir } from "./recursiveWalkDir.js";
export { extractWikiLinks, WikiLink } from "./extractWikiLinks.js";
export { parseFile } from "./parseFile.js";
export { parseMarkdownContent } from "./parseMarkdownContent.js";
export { getUniqueValues } from "./getUniqueValues.js";
export { generateFileIdFromPath } from "./generateFileIdFromPath.js";
export { getFileExtensionFromPath } from "./getFileExtensionFromPath.js";
export { defaultFilePathToUrl } from "./defaultFilePathToUrl.js";
export { extractFileSchemeFromObject } from "./extractFileSchemeFromObject.js";

0 comments on commit 6712e30

Please sign in to comment.