-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for working with markdown front matter (#642)
* Support for working with markdown front matter Fixes #641 Add support for working with markdown front matter. * **packages/core/src/frontmatter.ts** - Add `splitMarkdown` function to extract frontmatter and markdown content. - Add `updateFrontmatter` function to update frontmatter with new content. - Ensure `splitMarkdown` and `updateFrontmatter` handle various formats (yaml, json, toml). * **packages/core/src/frontmatter.test.ts** - Add tests for `splitMarkdown` function. - Add tests for `updateFrontmatter` function. * **packages/core/src/markdown.ts** - Import `splitMarkdown` and `updateFrontmatter` from `frontmatter.ts`. - Add `mergeFrontmatter` function to split markdown and update frontmatter. - Ensure `mergeFrontmatter` handles various formats (yaml, json, toml). --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/microsoft/genaiscript/issues/641?shareId=XXXX-XXXX-XXXX-XXXX). * removing toml support * typecheck issue * add global object * add MD type
- Loading branch information
Showing
25 changed files
with
619 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
title: Markdown | ||
sidebar: | ||
order: 9.2 | ||
keywords: markdown, mdx, frontmatter | ||
--- | ||
|
||
The `MD` class contains several helpers to work with [Markdown](https://www.markdownguide.org/cheat-sheet/) and [frontmatter text](https://jekyllrb.com/docs/front-matter/). | ||
|
||
The parser also support markdown variants like [MDX](https://mdxjs.com/). | ||
|
||
## `frontmatter` | ||
|
||
Extracts and parses the frontmatter text from a markdown file. Returns `undefined` if no frontmatter is not found or the parsing failed. Default format is `yaml`. | ||
|
||
```javascript | ||
const frontmatter = MD.frontmatter(text, "yaml") | ||
``` | ||
|
||
## `content` | ||
|
||
Extracts the markdown source without the frontmatter. | ||
|
||
```javascript | ||
const content = MD.content(text) | ||
``` | ||
|
||
## `updateFrontmatter` | ||
|
||
Merges frontmatter values into the existing in a markdown file. Use `null` value to delete fields. | ||
|
||
```javascript | ||
const updated = MD.updateFrontmatter(text, { title: "New Title" }) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,84 @@ | ||
import { objectFromMap } from "pdfjs-dist/types/src/shared/util" | ||
import { JSON5TryParse } from "./json5" | ||
import { TOMLTryParse } from "./toml" | ||
import { YAMLTryParse } from "./yaml" | ||
import { YAMLTryParse, YAMLStringify } from "./yaml" | ||
|
||
export function frontmatterTryParse( | ||
text: string, | ||
options?: { format: "yaml" | "json" | "toml" } | ||
): { end: number; value: any } { | ||
if (!text) return undefined | ||
|
||
options?: { format: "yaml" | "json" | "toml" | "text" } | ||
): { text: string; value: any; endLine?: number } | undefined { | ||
const { format = "yaml" } = options || {} | ||
const { frontmatter, endLine } = splitMarkdown(text) | ||
if (!frontmatter) return undefined | ||
|
||
let res: any | ||
switch (format) { | ||
case "text": | ||
res = frontmatter | ||
break | ||
case "json": | ||
res = JSON5TryParse(frontmatter) | ||
break | ||
case "toml": | ||
res = TOMLTryParse(frontmatter) | ||
break | ||
default: | ||
res = YAMLTryParse(frontmatter) | ||
break | ||
} | ||
return { text: frontmatter, value: res, endLine } | ||
} | ||
|
||
export function splitMarkdown(text: string): { | ||
frontmatter?: string | ||
endLine?: number | ||
content: string | ||
} { | ||
if (!text) return { content: text } | ||
const lines = text.split(/\r?\n/g) | ||
const delimiter = "---" | ||
if (lines[0] !== delimiter) return undefined | ||
if (lines[0] !== delimiter) return { content: text } | ||
let end = 1 | ||
while (end < lines.length) { | ||
if (lines[end] === delimiter) break | ||
end++ | ||
} | ||
if (end >= lines.length) return undefined | ||
const fm = lines.slice(1, end).join("\n") | ||
let res: any | ||
if (end >= lines.length) return { content: text } | ||
const frontmatter = lines.slice(1, end).join("\n") | ||
const content = lines.slice(end + 1).join("\n") | ||
return { frontmatter, content, endLine: end } | ||
} | ||
|
||
export function updateFrontmatter( | ||
text: string, | ||
newFrontmatter: any, | ||
options?: { format: "yaml" | "json" } | ||
): string { | ||
const { content = "" } = splitMarkdown(text) | ||
if (newFrontmatter === null) return content | ||
|
||
const frontmatter = frontmatterTryParse(text, options)?.value ?? {} | ||
|
||
// merge object | ||
for (const [key, value] of Object.entries(newFrontmatter ?? {})) { | ||
if (value === null) { | ||
delete frontmatter[key] | ||
} else { | ||
frontmatter[key] = value | ||
} | ||
} | ||
|
||
const { format = "yaml" } = options || {} | ||
let fm: string | ||
switch (format) { | ||
case "json": | ||
res = JSON5TryParse(fm) | ||
fm = JSON.stringify(frontmatter, null, 2) | ||
break | ||
case "toml": | ||
res = TOMLTryParse(fm) | ||
case "yaml": | ||
fm = YAMLStringify(frontmatter) | ||
break | ||
default: | ||
res = YAMLTryParse(fm) | ||
break | ||
throw new Error(`Unsupported format: ${format}`) | ||
} | ||
return res !== undefined ? { end: end + 1, value: res } : undefined | ||
return `---\n${fm}\n---\n${content}` | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.