Skip to content

Commit

Permalink
extracting links works in markdowndb
Browse files Browse the repository at this point in the history
  • Loading branch information
olayway committed Mar 31, 2023
1 parent eb567c1 commit 8531ed6
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/markdowndb/__mocks__/content/blog/blog1.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ title: My Test Mdx Blog 1
---

# My Test Mdx Blog 1

[[Blog2]]
3 changes: 3 additions & 0 deletions packages/markdowndb/src/lib/markdowndb.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ describe("MarkdownDB lib", () => {
// Ensure there is a "file_tags" table
expect(await db.schema.hasTable("file_tags")).toBe(true);

// Ensure there is a "links" table
expect(await db.schema.hasTable("links")).toBe(true);

const myMdDb = markdowndb.Database("markdown.db");

// Check if all files were indexed
Expand Down
58 changes: 57 additions & 1 deletion packages/markdowndb/src/lib/markdowndb.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import knex, { Knex } from "knex";
import * as fs from "fs";
import * as crypto from "crypto";
import knex, { Knex } from "knex";
import matter from "gray-matter";

import { DatabaseFile, DatabaseQuery } from "./types";
import extractWikiLinks from "../utils/extractWikiLinks";

import remarkWikiLink from "@flowershow/remark-wiki-link";

export const indexFolder = async (
dbPath: string,
Expand All @@ -22,19 +26,22 @@ export const indexFolder = async (
await createFilesTable(db);
await createTagsTable(db);
await createFileTagsTable(db);
await createLinksTable(db);

// Temporary, we don't want to handle updates now
// so database is refreshed every time the folder
// is indexed
await db("file_tags").del();
await db("tags").del();
await db("files").del();
await db("links").del();

const pathsToFiles = walkFolder(folderPath);

const filesToInsert = [];
const tagsToInsert = [];
const fileTagsToInsert = [];
const linksToInsert = [];

for (const pathToFile of pathsToFiles) {
let file;
Expand Down Expand Up @@ -121,6 +128,21 @@ const createFilesTable = async (db: Knex) => {
}
};

const createLinksTable = async (db: Knex) => {
const tableExists = await db.schema.hasTable("links");

if (!tableExists) {
await db.schema.createTable("links", (table) => {
table.string("_id").primary();
table.enum("type", ["normal", "embed"]).notNullable();
table.string("from");
table.string("to");
// table.foreign("from").references("files._id").onDelete("CASCADE");
// table.foreign("to").references("files._id").onDelete("CASCADE");
});
}
};

const createTagsTable = async (db: Knex) => {
const tableExists = await db.schema.hasTable("tags");

Expand Down Expand Up @@ -169,6 +191,40 @@ const createDatabaseFile: (path: string, folderPath: string) => DatabaseFile = (
metadata = data || null;
type = data.type || null;

// TODO pass this config as an argument, so that e.g. wikiLink doesn't have to be a dependency as it shouldnt
const extractWikiLinksConfig = {
remarkPlugins: [remarkWikiLink],
extractors: {
wikiLink: (node: any) => {
// TODO how to get wiki links of embed types in a better way?
// it should be possible, since we are adding { isType: "embed" } to tokens
const { href, src } = node.data?.hProperties || {};
return {
type: (href ? "normal" : "embed") as "normal" | "embed",
to: href ?? src,
};
},
},
};

// temporary function to sluggify file paths
const tempSluggify = (str: string) => {
return str
.replace(/\s+/g, "-")
.replace(/\.\w+$/, "")
.toLowerCase();
};

const links = extractWikiLinks({
source,
// TODO pass slug instead of file path as hrefs/srcs are sluggified too
// (where will we get it from?)
filePath: tempSluggify(`/${pathRelativeToFolder}`),
...extractWikiLinksConfig,
});

console.log({ links });

const segments = pathRelativeToFolder.split("/");
const filename = segments.at(-1).split(".")[0];

Expand Down
4 changes: 3 additions & 1 deletion packages/markdowndb/src/utils/extractWikiLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const resolveLink = (link: string, sourcePath?: string) => {
return link;
}
const dir = path.dirname(sourcePath);
return path.resolve(dir, link);
const resolved = path.resolve(dir, link);
console.log({ link, sourcePath, dir, resolved });
return resolved;
};

const extractWikiLinks = (options: ExtractWikiLinksConfig) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/remark-wiki-link/src/lib/pageResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const pageResolver = (
let page = isEmbed
? name
: name
.replace(/ /g, "-")
.replace(/\s+/g, "-")
.replace(/\/index$/, "")
.toLowerCase();
if (prefix) {
Expand Down
1 change: 1 addition & 0 deletions packages/remark-wiki-link/src/utils/getPermalinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const recursiveGetFiles = (dir) => {
return files;
};

// TODO slugify
export const getPermalinks = (markdownFolder) => {
const files = recursiveGetFiles(markdownFolder);
const permalinks = files.map((file) => {
Expand Down

0 comments on commit 8531ed6

Please sign in to comment.