Skip to content

Commit

Permalink
persist and maintain front matter
Browse files Browse the repository at this point in the history
- on import, track front matter in staging table if parsed
- work core Chronicles metadata (tags, title, dates) into front matter
- maintain front matter in documents (keep existing, merge Chronicles properties)
- refactor: migrate rest of client.documents to knex
- refactor: parse and stringify frontmatter tags as valid yaml
- add micromark front matter libraries and prefer them when parsing / serializing
  • Loading branch information
cloverich committed Dec 10, 2024
1 parent f494a5d commit 7f5635c
Show file tree
Hide file tree
Showing 12 changed files with 267 additions and 162 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@
"lucide-react": "^0.314.0",
"luxon": "^2.4.0",
"mdast-util-from-markdown": "^2.0.2",
"mdast-util-frontmatter": "^2.0.1",
"mdast-util-gfm": "^3.0.0",
"mdast-util-to-markdown": "^2.1.2",
"micromark-extension-frontmatter": "^2.0.0",
"micromark-extension-gfm": "^3.0.0",
"mobx": "^5.15.4",
"mobx-react-lite": "^2.0.7",
Expand Down
1 change: 1 addition & 0 deletions src/electron/migrations/20211005142122.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ CREATE TABLE IF NOT EXISTS "documents" (
"title" TEXT,
"content" TEXT NOT NULL,
"journal" TEXT NOT NULL,
"frontmatter" TEXT,
FOREIGN KEY ("journal") REFERENCES "journals" ("name") ON DELETE CASCADE ON UPDATE CASCADE
);

Expand Down
37 changes: 37 additions & 0 deletions src/markdown/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect } from "chai";
import fs from "fs";
import { describe, it } from "mocha";
import path from "path";
import yaml from "yaml";

import { slateToString, stringToSlate } from "./index.js";
import { dig, parseMarkdown, parseMarkdownForImport } from "./test-utils.js";
Expand Down Expand Up @@ -616,3 +617,39 @@ describe("Whacky shit", function () {
****[5 variations of Binary search (A Self Note)](https://leetcode.com/discuss/interview-question/1322500/5-variations-of-Binary-search-(A-Self-Note))****`;
});

describe.only("front matter parsing", function () {
const content = `---
title: 2024-09-29
tags: weekly-persona
createdAt: 2024-09-30T17:50:22.000Z
updatedAt: 2024-11-04T16:24:11.000Z
---
\#weekly-persona
Last week: [2024-09-22](../persona/0193acd4fa3574698c36c4514b907c70.md)
**I am on call this week** [On call week of 2024-09-30](../persona/0193acd4fa45731f81350d4443c1ed16.md)
## Monday
`;

// it.skip("should parse front matter", function () {
// const parsed = parseMarkdown(content);
// console.log(yaml.parse(parsed.children[0].value as string));
// });

it.only("test how to splice it back in", function () {
const parsed = parseMarkdown(content);
const frontMatter = yaml.parse(parsed.children[0].value as string);
const newFrontMatter = yaml.stringify({
...frontMatter,
title: "2024-09-29",
});

// ok, it needs --- added
console.log(newFrontMatter);
});
});
16 changes: 12 additions & 4 deletions src/markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import * as mdast from "mdast";
export { slateToMdast } from "./remark-slate-transformer/transformers/slate-to-mdast.js";

import { fromMarkdown } from "mdast-util-from-markdown";
import {
frontmatterFromMarkdown,
frontmatterToMarkdown,
} from "mdast-util-frontmatter";
import { gfmFromMarkdown, gfmToMarkdown } from "mdast-util-gfm";
import { toMarkdown } from "mdast-util-to-markdown";
import { frontmatter } from "micromark-extension-frontmatter";
import { gfm } from "micromark-extension-gfm";
import { ofmTagFromMarkdown } from "./mdast-util-ofm-tag";
import { ofmWikilinkFromMarkdown } from "./mdast-util-ofm-wikilink";
Expand Down Expand Up @@ -53,25 +58,28 @@ function wrapImages(tree: mdast.Root) {
// to Chronicles tags and markdown links. Future versions may support these properly.
export const parseMarkdownForImport = (markdown: string): mdast.Root => {
return fromMarkdown(markdown, {
extensions: [gfm(), ofmTag(), ofmWikilink()],
extensions: [gfm(), ofmTag(), ofmWikilink(), frontmatter(["yaml"])],
mdastExtensions: [
gfmFromMarkdown(),
ofmTagFromMarkdown(),
ofmWikilinkFromMarkdown(),
// https://github.com/micromark/micromark-extension-frontmatter?tab=readme-ov-file#preset
// todo: support toml (need toml parser)
frontmatterFromMarkdown(["yaml"]),
],
});
};

export const parseMarkdown = (markdown: string): mdast.Root => {
return fromMarkdown(markdown, {
extensions: [gfm()],
mdastExtensions: [gfmFromMarkdown()],
extensions: [gfm(), frontmatter(["yaml"])],
mdastExtensions: [gfmFromMarkdown(), frontmatterFromMarkdown(["yaml"])],
});
};

export const mdastToString = (tree: mdast.Nodes) => {
return toMarkdown(tree, {
extensions: [gfmToMarkdown() as any],
extensions: [gfmToMarkdown() as any, frontmatterToMarkdown(["yaml"])],
bullet: "-",
emphasis: "_",
});
Expand Down
Loading

0 comments on commit 7f5635c

Please sign in to comment.