@astrojs/markdown-remark 6.0.0-alpha.1 → 6.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1,20 @@
1
1
  export declare function isFrontmatterValid(frontmatter: Record<string, any>): boolean;
2
+ export declare function extractFrontmatter(code: string): string | undefined;
3
+ export interface ParseFrontmatterOptions {
4
+ /**
5
+ * How the frontmatter should be handled in the returned `content` string.
6
+ * - `preserve`: Keep the frontmatter.
7
+ * - `remove`: Remove the frontmatter.
8
+ * - `empty-with-spaces`: Replace the frontmatter with empty spaces. (preserves sourcemap line/col/offset)
9
+ * - `empty-with-lines`: Replace the frontmatter with empty line breaks. (preserves sourcemap line/col)
10
+ *
11
+ * @default 'remove'
12
+ */
13
+ frontmatter: 'preserve' | 'remove' | 'empty-with-spaces' | 'empty-with-lines';
14
+ }
15
+ export interface ParseFrontmatterResult {
16
+ frontmatter: Record<string, any>;
17
+ rawFrontmatter: string;
18
+ content: string;
19
+ }
20
+ export declare function parseFrontmatter(code: string, options?: ParseFrontmatterOptions): ParseFrontmatterResult;
@@ -1,3 +1,4 @@
1
+ import yaml from "js-yaml";
1
2
  function isFrontmatterValid(frontmatter) {
2
3
  try {
3
4
  JSON.stringify(frontmatter);
@@ -6,6 +7,43 @@ function isFrontmatterValid(frontmatter) {
6
7
  }
7
8
  return typeof frontmatter === "object" && frontmatter !== null;
8
9
  }
10
+ const frontmatterRE = /^---(.*?)^---/ms;
11
+ function extractFrontmatter(code) {
12
+ return frontmatterRE.exec(code)?.[1];
13
+ }
14
+ function parseFrontmatter(code, options) {
15
+ const rawFrontmatter = extractFrontmatter(code);
16
+ if (rawFrontmatter == null) {
17
+ return { frontmatter: {}, rawFrontmatter: "", content: code };
18
+ }
19
+ const parsed = yaml.load(rawFrontmatter);
20
+ const frontmatter = parsed && typeof parsed === "object" ? parsed : {};
21
+ let content;
22
+ switch (options?.frontmatter ?? "remove") {
23
+ case "preserve":
24
+ content = code;
25
+ break;
26
+ case "remove":
27
+ content = code.replace(`---${rawFrontmatter}---`, "");
28
+ break;
29
+ case "empty-with-spaces":
30
+ content = code.replace(
31
+ `---${rawFrontmatter}---`,
32
+ ` ${rawFrontmatter.replace(/[^\r\n]/g, " ")} `
33
+ );
34
+ break;
35
+ case "empty-with-lines":
36
+ content = code.replace(`---${rawFrontmatter}---`, rawFrontmatter.replace(/[^\r\n]/g, ""));
37
+ break;
38
+ }
39
+ return {
40
+ frontmatter,
41
+ rawFrontmatter,
42
+ content
43
+ };
44
+ }
9
45
  export {
10
- isFrontmatterValid
46
+ extractFrontmatter,
47
+ isFrontmatterValid,
48
+ parseFrontmatter
11
49
  };
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@ export { rehypeHeadingIds } from './rehype-collect-headings.js';
3
3
  export { remarkCollectImages } from './remark-collect-images.js';
4
4
  export { rehypePrism } from './rehype-prism.js';
5
5
  export { rehypeShiki } from './rehype-shiki.js';
6
- export { isFrontmatterValid } from './frontmatter.js';
6
+ export { isFrontmatterValid, extractFrontmatter, parseFrontmatter, type ParseFrontmatterOptions, type ParseFrontmatterResult, } from './frontmatter.js';
7
7
  export { createShikiHighlighter, type ShikiHighlighter, type CreateShikiHighlighterOptions, type ShikiHighlighterHighlightOptions, } from './shiki.js';
8
8
  export * from './types.js';
9
9
  export declare const markdownConfigDefaults: Required<AstroMarkdownOptions>;
package/dist/index.js CHANGED
@@ -16,7 +16,11 @@ import { rehypeHeadingIds as rehypeHeadingIds2 } from "./rehype-collect-headings
16
16
  import { remarkCollectImages as remarkCollectImages2 } from "./remark-collect-images.js";
17
17
  import { rehypePrism as rehypePrism2 } from "./rehype-prism.js";
18
18
  import { rehypeShiki as rehypeShiki2 } from "./rehype-shiki.js";
19
- import { isFrontmatterValid } from "./frontmatter.js";
19
+ import {
20
+ isFrontmatterValid,
21
+ extractFrontmatter,
22
+ parseFrontmatter
23
+ } from "./frontmatter.js";
20
24
  import {
21
25
  createShikiHighlighter
22
26
  } from "./shiki.js";
@@ -131,8 +135,10 @@ ${err.message}`;
131
135
  export {
132
136
  createMarkdownProcessor,
133
137
  createShikiHighlighter,
138
+ extractFrontmatter,
134
139
  isFrontmatterValid,
135
140
  markdownConfigDefaults,
141
+ parseFrontmatter,
136
142
  rehypeHeadingIds2 as rehypeHeadingIds,
137
143
  rehypePrism2 as rehypePrism,
138
144
  rehypeShiki2 as rehypeShiki,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/markdown-remark",
3
- "version": "6.0.0-alpha.1",
3
+ "version": "6.0.0-beta.2",
4
4
  "type": "module",
5
5
  "author": "withastro",
6
6
  "license": "MIT",
@@ -26,17 +26,18 @@
26
26
  ],
27
27
  "dependencies": {
28
28
  "github-slugger": "^2.0.0",
29
- "hast-util-from-html": "^2.0.2",
29
+ "hast-util-from-html": "^2.0.3",
30
30
  "hast-util-to-text": "^4.0.2",
31
31
  "import-meta-resolve": "^4.1.0",
32
+ "js-yaml": "^4.1.0",
32
33
  "mdast-util-definitions": "^6.0.0",
33
34
  "rehype-raw": "^7.0.0",
34
- "rehype-stringify": "^10.0.0",
35
+ "rehype-stringify": "^10.0.1",
35
36
  "remark-gfm": "^4.0.0",
36
37
  "remark-parse": "^11.0.0",
37
- "remark-rehype": "^11.1.0",
38
+ "remark-rehype": "^11.1.1",
38
39
  "remark-smartypants": "^3.0.2",
39
- "shiki": "^1.16.1",
40
+ "shiki": "^1.21.0",
40
41
  "unified": "^11.0.5",
41
42
  "unist-util-remove-position": "^5.0.0",
42
43
  "unist-util-visit": "^5.0.0",
@@ -45,12 +46,13 @@
45
46
  "@astrojs/prism": "3.1.0"
46
47
  },
47
48
  "devDependencies": {
48
- "@types/estree": "^1.0.5",
49
+ "@types/estree": "^1.0.6",
49
50
  "@types/hast": "^3.0.4",
51
+ "@types/js-yaml": "^4.0.9",
50
52
  "@types/mdast": "^4.0.4",
51
53
  "@types/unist": "^3.0.3",
52
54
  "esbuild": "^0.21.5",
53
- "mdast-util-mdx-expression": "^2.0.0",
55
+ "mdast-util-mdx-expression": "^2.0.1",
54
56
  "astro-scripts": "0.0.14"
55
57
  },
56
58
  "publishConfig": {