From 8fcda1db4fc2b19eade703c25f91438eb6e69d30 Mon Sep 17 00:00:00 2001 From: mohamed yahia Date: Wed, 15 Nov 2023 20:34:34 +0200 Subject: [PATCH] Add process function and add some tests --- src/lib/process.spec.ts | 17 ++++++++--- src/lib/process.ts | 68 ++++++++++++++++++++++++++++++----------- tsconfig.spec.json | 3 +- 3 files changed, 66 insertions(+), 22 deletions(-) diff --git a/src/lib/process.spec.ts b/src/lib/process.spec.ts index 97bbd98..962e490 100644 --- a/src/lib/process.spec.ts +++ b/src/lib/process.spec.ts @@ -1,14 +1,23 @@ import { parseFile2 } from "./process"; -import Path from 'path'; +import Path from "path"; describe("Can parse a file and get file info", () => { const pathToContentFixture = "__mocks__/content"; test("can parse a file", async () => { - const filePath = 'index.mdx'; + const filePath = "index.mdx"; const fullPath = Path.join(pathToContentFixture, filePath); - const fileInfo = await parseFile2(fullPath) + const fileInfo = await parseFile2(fullPath); + expect(fileInfo.file_path).toBe(fullPath); - // expect(fileInfo.metadata?.title).toBe('Homepage') + expect(fileInfo.extension).toBe("mdx"); + expect(fileInfo.tags).toEqual(["tag1", "tag2", "tag3"]); + expect(fileInfo.metadata).toEqual({ + title: "Homepage", + tags: ["tag1", "tag2", "tag3"], + }); + expect(fileInfo.links).toEqual([ + { linkSrc: "blog0.mdx", linkType: "normal" }, + ]); }); }); diff --git a/src/lib/process.ts b/src/lib/process.ts index a5b5fef..91f06e2 100644 --- a/src/lib/process.ts +++ b/src/lib/process.ts @@ -1,11 +1,11 @@ import crypto from "crypto"; import fs from "fs"; -import path from "path"; import { parseFile, WikiLink } from "../utils/index.js"; // this file is an extraction of the file info parsing from markdowndb.ts without any sql stuff -export function parseFile2(filePath:string) { +// TODO: add back (as an option) - providing a "root folder" path for resolve +export function parseFile2(filePath: string) { // gets key file info if any e.g. extension (file size??) const encodedPath = Buffer.from(filePath, "utf-8").toString(); @@ -16,7 +16,7 @@ export function parseFile2(filePath:string) { // Path.extname const [, extension] = filePath.match(/.(\w+)$/) || []; - let fileInfo = { + let fileInfo: FileInfo = { _id: id, file_path: filePath, extension, @@ -24,18 +24,15 @@ export function parseFile2(filePath:string) { filetype: null, metadata: {}, tags: [], - links: [] - } + links: [], + }; - return fileInfo; - - // TODO: add back later // if not a file type we can parse exit here ... // if (extension ! in list of supported extensions exit now ...) - - // url_path - // const pathRelativeToFolder = path.relative(folderPath, filePath); - // const urlPath = pIathToUrlResolver(pathRelativeToFolder); + const isExtensionSupported = extension === "md" || extension === "mdx"; + if (!isExtensionSupported) { + return fileInfo; + } // metadata, tags, links const source: string = fs.readFileSync(filePath, { @@ -44,13 +41,50 @@ export function parseFile2(filePath:string) { }); const { metadata, links } = parseFile(source, { - permalinks: [] + permalinks: [], }); - const filetype = metadata?.type || null; + fileInfo.metadata = metadata; + fileInfo.links = links; - // fileInfo.links = links; + const filetype = metadata?.type || null; + fileInfo.filetype = filetype; const tags = metadata?.tags || []; fileInfo.tags = tags; - fileInfo.metadata = metadata; -} \ No newline at end of file + + return fileInfo; +} + +// let's put types here - later we'll refactor them out ... +interface FileInfo extends File { + tags: Tag[]; + links: WikiLink[]; +} + +export interface File { + _id: string; + file_path: string; + extension: string; + url_path: string | null; + filetype: string | null; + metadata: MetaData | null; +} + +export type MetaData = { + [key: string]: any; +}; + +export interface Link { + link_type: "normal" | "embed"; + from: string; + to: string; +} + +export interface Tag { + name: string; +} + +export interface FileTag { + tag: string; + file: string; +} diff --git a/tsconfig.spec.json b/tsconfig.spec.json index 7453ec9..2ccfdb1 100644 --- a/tsconfig.spec.json +++ b/tsconfig.spec.json @@ -15,6 +15,7 @@ "src/**/*.spec.js", "src/**/*.test.jsx", "src/**/*.spec.jsx", - "src/**/*.d.ts" + "src/**/*.d.ts", + "src/lib/process.ts" ] }