Skip to content

Commit

Permalink
Add process function and add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedsalem401 committed Nov 15, 2023
1 parent 309757e commit 8fcda1d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 22 deletions.
17 changes: 13 additions & 4 deletions 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" },
]);
});
});
68 changes: 51 additions & 17 deletions 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();
Expand All @@ -16,26 +16,23 @@ export function parseFile2(filePath:string) {
// Path.extname
const [, extension] = filePath.match(/.(\w+)$/) || [];

let fileInfo = {
let fileInfo: FileInfo = {
_id: id,
file_path: filePath,
extension,
url_path: null,
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, {
Expand All @@ -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;
}

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;
}
3 changes: 2 additions & 1 deletion tsconfig.spec.json
Expand Up @@ -15,6 +15,7 @@
"src/**/*.spec.js",
"src/**/*.test.jsx",
"src/**/*.spec.jsx",
"src/**/*.d.ts"
"src/**/*.d.ts",
"src/lib/process.ts"
]
}

0 comments on commit 8fcda1d

Please sign in to comment.