@astrojs/markdown-remark 6.0.0-beta.1 → 6.0.0-beta.3

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";
@@ -28,7 +32,8 @@ const markdownConfigDefaults = {
28
32
  theme: "github-dark",
29
33
  themes: {},
30
34
  wrap: false,
31
- transformers: []
35
+ transformers: [],
36
+ langAlias: {}
32
37
  },
33
38
  remarkPlugins: [],
34
39
  rehypePlugins: [],
@@ -131,8 +136,10 @@ ${err.message}`;
131
136
  export {
132
137
  createMarkdownProcessor,
133
138
  createShikiHighlighter,
139
+ extractFrontmatter,
134
140
  isFrontmatterValid,
135
141
  markdownConfigDefaults,
142
+ parseFrontmatter,
136
143
  rehypeHeadingIds2 as rehypeHeadingIds,
137
144
  rehypePrism2 as rehypePrism,
138
145
  rehypeShiki2 as rehypeShiki,
@@ -6,7 +6,8 @@ const rehypeShiki = (config) => {
6
6
  highlighterAsync ??= createShikiHighlighter({
7
7
  langs: config?.langs,
8
8
  theme: config?.theme,
9
- themes: config?.themes
9
+ themes: config?.themes,
10
+ langAlias: config?.langAlias
10
11
  });
11
12
  const highlighter = await highlighterAsync;
12
13
  await highlightCodeBlocks(tree, (code, language, options) => {
package/dist/shiki.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Root } from 'hast';
2
- import { type LanguageRegistration, type ShikiTransformer, type ThemeRegistration, type ThemeRegistrationRaw } from 'shiki';
2
+ import { type HighlighterCoreOptions, type LanguageRegistration, type ShikiTransformer, type ThemeRegistration, type ThemeRegistrationRaw } from 'shiki';
3
3
  import type { ThemePresets } from './types.js';
4
4
  export interface ShikiHighlighter {
5
5
  codeToHast(code: string, lang?: string, options?: ShikiHighlighterHighlightOptions): Promise<Root>;
@@ -9,6 +9,7 @@ export interface CreateShikiHighlighterOptions {
9
9
  langs?: LanguageRegistration[];
10
10
  theme?: ThemePresets | ThemeRegistration | ThemeRegistrationRaw;
11
11
  themes?: Record<string, ThemePresets | ThemeRegistration | ThemeRegistrationRaw>;
12
+ langAlias?: HighlighterCoreOptions['langAlias'];
12
13
  }
13
14
  export interface ShikiHighlighterHighlightOptions {
14
15
  /**
@@ -39,4 +40,4 @@ export interface ShikiHighlighterHighlightOptions {
39
40
  */
40
41
  meta?: string;
41
42
  }
42
- export declare function createShikiHighlighter({ langs, theme, themes, }?: CreateShikiHighlighterOptions): Promise<ShikiHighlighter>;
43
+ export declare function createShikiHighlighter({ langs, theme, themes, langAlias, }?: CreateShikiHighlighterOptions): Promise<ShikiHighlighter>;
package/dist/shiki.js CHANGED
@@ -4,24 +4,30 @@ import {
4
4
  isSpecialLang
5
5
  } from "shiki";
6
6
  let _cssVariablesTheme;
7
- const cssVariablesTheme = () => _cssVariablesTheme ?? (_cssVariablesTheme = createCssVariablesTheme({ variablePrefix: "--astro-code-" }));
7
+ const cssVariablesTheme = () => _cssVariablesTheme ?? (_cssVariablesTheme = createCssVariablesTheme({
8
+ variablePrefix: "--astro-code-"
9
+ }));
8
10
  async function createShikiHighlighter({
9
11
  langs = [],
10
12
  theme = "github-dark",
11
- themes = {}
13
+ themes = {},
14
+ langAlias = {}
12
15
  } = {}) {
13
16
  theme = theme === "css-variables" ? cssVariablesTheme() : theme;
14
17
  const highlighter = await createHighlighter({
15
18
  langs: ["plaintext", ...langs],
19
+ langAlias,
16
20
  themes: Object.values(themes).length ? Object.values(themes) : [theme]
17
21
  });
18
22
  async function highlight(code, lang = "plaintext", options, to) {
23
+ const resolvedLang = langAlias[lang] ?? lang;
19
24
  const loadedLanguages = highlighter.getLoadedLanguages();
20
- if (!isSpecialLang(lang) && !loadedLanguages.includes(lang)) {
25
+ if (!isSpecialLang(lang) && !loadedLanguages.includes(resolvedLang)) {
21
26
  try {
22
- await highlighter.loadLanguage(lang);
27
+ await highlighter.loadLanguage(resolvedLang);
23
28
  } catch (_err) {
24
- console.warn(`[Shiki] The language "${lang}" doesn't exist, falling back to "plaintext".`);
29
+ const langStr = lang === resolvedLang ? `"${lang}"` : `"${lang}" (aliased to "${resolvedLang}")`;
30
+ console.warn(`[Shiki] The language ${langStr} doesn't exist, falling back to "plaintext".`);
25
31
  lang = "plaintext";
26
32
  }
27
33
  }
@@ -58,7 +64,7 @@ async function createShikiHighlighter({
58
64
  }
59
65
  },
60
66
  line(node) {
61
- if (lang === "diff") {
67
+ if (resolvedLang === "diff") {
62
68
  const innerSpanNode = node.children[0];
63
69
  const innerSpanTextNode = innerSpanNode?.type === "element" && innerSpanNode.children?.[0];
64
70
  if (innerSpanTextNode && innerSpanTextNode.type === "text") {
package/dist/types.d.ts CHANGED
@@ -20,7 +20,7 @@ export type RehypePlugin<PluginParameters extends any[] = any[]> = unified.Plugi
20
20
  export type RehypePlugins = (string | [string, any] | RehypePlugin | [RehypePlugin, any])[];
21
21
  export type RemarkRehype = RemarkRehypeOptions;
22
22
  export type ThemePresets = BuiltinTheme | 'css-variables';
23
- export interface ShikiConfig extends Pick<CreateShikiHighlighterOptions, 'langs' | 'theme' | 'themes'>, Pick<ShikiHighlighterHighlightOptions, 'defaultColor' | 'wrap' | 'transformers'> {
23
+ export interface ShikiConfig extends Pick<CreateShikiHighlighterOptions, 'langs' | 'theme' | 'themes' | 'langAlias'>, Pick<ShikiHighlighterHighlightOptions, 'defaultColor' | 'wrap' | 'transformers'> {
24
24
  }
25
25
  export interface AstroMarkdownOptions {
26
26
  syntaxHighlight?: 'shiki' | 'prism' | false;
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@astrojs/markdown-remark",
3
- "version": "6.0.0-beta.1",
3
+ "version": "6.0.0-beta.3",
4
4
  "type": "module",
5
5
  "author": "withastro",
6
6
  "license": "MIT",
7
7
  "repository": {
8
8
  "type": "git",
9
- "url": "https://github.com/withastro/astro.git",
9
+ "url": "git+https://github.com/withastro/astro.git",
10
10
  "directory": "packages/markdown/remark"
11
11
  },
12
12
  "bugs": "https://github.com/withastro/astro/issues",
@@ -26,31 +26,33 @@
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.2",
40
+ "shiki": "^1.23.1",
40
41
  "unified": "^11.0.5",
41
42
  "unist-util-remove-position": "^5.0.0",
42
43
  "unist-util-visit": "^5.0.0",
43
44
  "unist-util-visit-parents": "^6.0.1",
44
45
  "vfile": "^6.0.3",
45
- "@astrojs/prism": "3.1.0"
46
+ "@astrojs/prism": "3.2.0-beta.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": {