@astrojs/markdown-remark 0.7.0 → 0.8.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @astrojs/markdown-remark
2
2
 
3
+ ## 0.8.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#2970](https://github.com/withastro/astro/pull/2970) [`b835e285`](https://github.com/withastro/astro/commit/b835e285defb4f31fc5ac1039c7f607c07f3c00b) Thanks [@JuanM04](https://github.com/JuanM04)! - Improved type checking
8
+
9
+ ## 0.8.1
10
+
11
+ ### Patch Changes
12
+
13
+ - [#2971](https://github.com/withastro/astro/pull/2971) [`ad3c3916`](https://github.com/withastro/astro/commit/ad3c391696c5b9cc350a22831717682e73e25776) Thanks [@JuanM04](https://github.com/JuanM04)! - Escape expressions when mode == 'md'
14
+
15
+ ## 0.8.0
16
+
17
+ ### Minor Changes
18
+
19
+ - [`e425f896`](https://github.com/withastro/astro/commit/e425f896b668d98033ad3b998b50c1f28bc7f6ee) Thanks [@FredKSchott](https://github.com/FredKSchott)! - Update config options to resepect [RFC0019](https://github.com/withastro/rfcs/blob/main/proposals/0019-config-finalization.md)
20
+
3
21
  ## 0.7.0
4
22
 
5
23
  ### Minor Changes
@@ -187,10 +205,10 @@
187
205
 
188
206
  ```js
189
207
  export default {
190
- markdownOptions: {
191
- remarkPlugins: ['remark-slug', ['remark-autolink-headings', { behavior: 'prepend' }]],
192
- rehypePlugins: ['rehype-slug', ['rehype-autolink-headings', { behavior: 'prepend' }]],
193
- },
208
+ markdownOptions: {
209
+ remarkPlugins: ['remark-slug', ['remark-autolink-headings', { behavior: 'prepend' }]],
210
+ rehypePlugins: ['rehype-slug', ['rehype-autolink-headings', { behavior: 'prepend' }]],
211
+ },
194
212
  };
195
213
  ```
196
214
 
package/dist/index.d.ts CHANGED
@@ -1,21 +1,9 @@
1
- import type { AstroMarkdownOptions, MarkdownRenderingOptions } from './types';
2
- export { AstroMarkdownOptions, MarkdownRenderingOptions };
3
- /** Internal utility for rendering a full markdown file and extracting Frontmatter data */
4
- export declare function renderMarkdownWithFrontmatter(contents: string, opts?: MarkdownRenderingOptions | null): Promise<{
5
- frontmatter: {
6
- [key: string]: any;
7
- };
8
- metadata: {
9
- headers: any[];
10
- source: string;
11
- html: string;
12
- };
13
- code: string;
14
- }>;
1
+ import type { MarkdownRenderingOptions } from './types';
2
+ export * from './types.js';
15
3
  export declare const DEFAULT_REMARK_PLUGINS: string[];
16
4
  export declare const DEFAULT_REHYPE_PLUGINS: string[];
17
5
  /** Shared utility for rendering markdown */
18
- export declare function renderMarkdown(content: string, opts?: MarkdownRenderingOptions | null): Promise<{
6
+ export declare function renderMarkdown(content: string, opts: MarkdownRenderingOptions): Promise<{
19
7
  metadata: {
20
8
  headers: any[];
21
9
  source: string;
@@ -23,4 +11,3 @@ export declare function renderMarkdown(content: string, opts?: MarkdownRendering
23
11
  };
24
12
  code: string;
25
13
  }>;
26
- export default renderMarkdownWithFrontmatter;
package/dist/index.js CHANGED
@@ -1,22 +1,3 @@
1
- var __defProp = Object.defineProperty;
2
- var __defProps = Object.defineProperties;
3
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
- var __spreadValues = (a, b) => {
9
- for (var prop in b || (b = {}))
10
- if (__hasOwnProp.call(b, prop))
11
- __defNormalProp(a, prop, b[prop]);
12
- if (__getOwnPropSymbols)
13
- for (var prop of __getOwnPropSymbols(b)) {
14
- if (__propIsEnum.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- }
17
- return a;
18
- };
19
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
1
  import createCollectHeaders from "./rehype-collect-headers.js";
21
2
  import scopedStyles from "./remark-scoped-styles.js";
22
3
  import { remarkExpressions, loadRemarkExpressions } from "./remark-expressions.js";
@@ -34,25 +15,17 @@ import markdown from "remark-parse";
34
15
  import markdownToHtml from "remark-rehype";
35
16
  import rehypeStringify from "rehype-stringify";
36
17
  import rehypeRaw from "rehype-raw";
37
- import matter from "gray-matter";
38
- async function renderMarkdownWithFrontmatter(contents, opts) {
39
- const { data: frontmatter, content } = matter(contents);
40
- const value = await renderMarkdown(content, opts);
41
- return __spreadProps(__spreadValues({}, value), { frontmatter });
42
- }
18
+ export * from "./types.js";
43
19
  const DEFAULT_REMARK_PLUGINS = ["remark-gfm", "remark-smartypants"];
44
20
  const DEFAULT_REHYPE_PLUGINS = ["rehype-slug"];
45
21
  async function renderMarkdown(content, opts) {
46
22
  var _a;
47
- let { remarkPlugins = [], rehypePlugins = [] } = opts ?? {};
48
- const scopedClassName = (_a = opts == null ? void 0 : opts.$) == null ? void 0 : _a.scopedClassName;
49
- const mode = (opts == null ? void 0 : opts.mode) ?? "mdx";
50
- const syntaxHighlight = (opts == null ? void 0 : opts.syntaxHighlight) ?? "shiki";
51
- const shikiConfig = (opts == null ? void 0 : opts.shikiConfig) ?? {};
23
+ let { mode, syntaxHighlight, shikiConfig, remarkPlugins, rehypePlugins } = opts;
24
+ const scopedClassName = (_a = opts.$) == null ? void 0 : _a.scopedClassName;
52
25
  const isMDX = mode === "mdx";
53
26
  const { headers, rehypeCollectHeaders } = createCollectHeaders();
54
27
  await Promise.all([loadRemarkExpressions(), loadRemarkJsx()]);
55
- let parser = unified().use(markdown).use(isMDX ? [remarkJsx] : []).use(isMDX ? [remarkExpressions] : []).use([remarkUnwrap]);
28
+ let parser = unified().use(markdown).use(isMDX ? [remarkJsx, remarkExpressions] : []).use([remarkUnwrap]);
56
29
  if (remarkPlugins.length === 0 && rehypePlugins.length === 0) {
57
30
  remarkPlugins = [...DEFAULT_REMARK_PLUGINS];
58
31
  rehypePlugins = [...DEFAULT_REHYPE_PLUGINS];
@@ -70,11 +43,19 @@ async function renderMarkdown(content, opts) {
70
43
  } else if (syntaxHighlight === "prism") {
71
44
  parser.use([remarkPrism(scopedClassName)]);
72
45
  }
73
- parser.use([[markdownToHtml, { allowDangerousHtml: true, passThrough: ["raw", "mdxTextExpression", "mdxJsxTextElement", "mdxJsxFlowElement"] }]]);
46
+ parser.use([
47
+ [
48
+ markdownToHtml,
49
+ {
50
+ allowDangerousHtml: true,
51
+ passThrough: ["raw", "mdxTextExpression", "mdxJsxTextElement", "mdxJsxFlowElement"]
52
+ }
53
+ ]
54
+ ]);
74
55
  loadedRehypePlugins.forEach(([plugin, opts2]) => {
75
56
  parser.use([[plugin, opts2]]);
76
57
  });
77
- parser.use(isMDX ? [rehypeJsx] : []).use(isMDX ? [rehypeExpressions] : []).use(isMDX ? [] : [rehypeRaw]).use(isMDX ? [rehypeEscape] : []).use(rehypeIslands);
58
+ parser.use(isMDX ? [rehypeJsx, rehypeExpressions] : [rehypeRaw]).use(rehypeEscape).use(rehypeIslands);
78
59
  let result;
79
60
  try {
80
61
  const vfile = await parser.use([rehypeCollectHeaders]).use(rehypeStringify, { allowDangerousHtml: true }).process(content);
@@ -88,11 +69,8 @@ async function renderMarkdown(content, opts) {
88
69
  code: result.toString()
89
70
  };
90
71
  }
91
- var src_default = renderMarkdownWithFrontmatter;
92
72
  export {
93
73
  DEFAULT_REHYPE_PLUGINS,
94
74
  DEFAULT_REMARK_PLUGINS,
95
- src_default as default,
96
- renderMarkdown,
97
- renderMarkdownWithFrontmatter
75
+ renderMarkdown
98
76
  };
@@ -1,3 +1,2 @@
1
1
  import * as unified from 'unified';
2
- import type { Plugin } from './types';
3
- export declare function loadPlugins(items: Plugin[]): Promise<[unified.Plugin, any?]>[];
2
+ export declare function loadPlugins(items: (string | [string, any] | unified.Plugin<any[], any> | [unified.Plugin<any[], any>, any])[]): Promise<[unified.Plugin, any?]>[];
@@ -26,7 +26,10 @@ function rehypeIslands() {
26
26
  visit(el, "text", (child, index, parent) => {
27
27
  if (child.type === "text") {
28
28
  if (parent && child.value.indexOf("<!--") > -1 && index != null) {
29
- parent.children.splice(index, 1, __spreadProps(__spreadValues({}, child), { type: "comment", value: child.value.replace("<!--", "").replace("-->", "").trim() }));
29
+ parent.children.splice(index, 1, __spreadProps(__spreadValues({}, child), {
30
+ type: "comment",
31
+ value: child.value.replace("<!--", "").replace("-->", "").trim()
32
+ }));
30
33
  return [SKIP, index];
31
34
  }
32
35
  child.value = child.value.replace(/\n+/g, "");
@@ -1,30 +1,3 @@
1
- import type * as shiki from 'shiki';
2
- export interface ShikiConfig {
3
- /**
4
- * The languages loaded to Shiki.
5
- * Supports all languages listed here: https://github.com/shikijs/shiki/blob/main/docs/languages.md#all-languages
6
- * Instructions for loading a custom language: https://github.com/shikijs/shiki/blob/main/docs/languages.md#supporting-your-own-languages-with-shiki
7
- *
8
- * @default []
9
- */
10
- langs?: shiki.ILanguageRegistration[];
11
- /**
12
- * The styling theme.
13
- * Supports all themes listed here: https://github.com/shikijs/shiki/blob/main/docs/themes.md#all-themes
14
- * Instructions for loading a custom theme: https://github.com/shikijs/shiki/blob/main/docs/themes.md#loading-theme
15
- *
16
- * @default "github-dark"
17
- */
18
- theme?: shiki.IThemeRegistration;
19
- /**
20
- * Enable word wrapping.
21
- * - true: enabled.
22
- * - false: enabled.
23
- * - null: All overflow styling removed. Code will overflow the element by default.
24
- *
25
- * @default false
26
- */
27
- wrap?: boolean | null;
28
- }
1
+ import type { ShikiConfig } from './types.js';
29
2
  declare const remarkShiki: ({ langs, theme, wrap }: ShikiConfig, scopedClassName?: string | null | undefined) => Promise<() => (tree: any) => void>;
30
3
  export default remarkShiki;
@@ -1,7 +1,7 @@
1
1
  import { getHighlighter } from "shiki";
2
2
  import { visit } from "unist-util-visit";
3
3
  const highlighterCacheAsync = /* @__PURE__ */ new Map();
4
- const remarkShiki = async ({ langs = [], theme = "github-dark", wrap = false }, scopedClassName) => {
4
+ const remarkShiki = async ({ langs, theme, wrap }, scopedClassName) => {
5
5
  const cacheID = typeof theme === "string" ? theme : theme.name;
6
6
  let highlighterAsync = highlighterCacheAsync.get(cacheID);
7
7
  if (!highlighterAsync) {
package/dist/types.d.ts CHANGED
@@ -1,14 +1,26 @@
1
1
  import type * as unified from 'unified';
2
- import type { ShikiConfig } from './remark-shiki';
3
- export declare type Plugin = string | [string, any] | unified.Plugin | [unified.Plugin, any];
2
+ import type * as mdast from 'mdast';
3
+ import type * as hast from 'hast';
4
+ import type { ILanguageRegistration, IThemeRegistration, Theme } from 'shiki';
5
+ export type { Node } from 'unist';
6
+ export declare type RemarkPlugin<PluginParameters extends any[] = any[]> = unified.Plugin<PluginParameters, mdast.Root>;
7
+ export declare type RemarkPlugins = (string | [string, any] | RemarkPlugin | [RemarkPlugin, any])[];
8
+ export declare type RehypePlugin<PluginParameters extends any[] = any[]> = unified.Plugin<PluginParameters, hast.Root>;
9
+ export declare type RehypePlugins = (string | [string, any] | RehypePlugin | [RehypePlugin, any])[];
10
+ export interface ShikiConfig {
11
+ langs: ILanguageRegistration[];
12
+ theme: Theme | IThemeRegistration;
13
+ wrap: boolean | null;
14
+ }
4
15
  export interface AstroMarkdownOptions {
5
- mode?: 'md' | 'mdx';
6
- syntaxHighlight?: 'shiki' | 'prism' | false;
7
- shikiConfig?: ShikiConfig;
8
- remarkPlugins?: Plugin[];
9
- rehypePlugins?: Plugin[];
16
+ mode: 'md' | 'mdx';
17
+ drafts: boolean;
18
+ syntaxHighlight: 'shiki' | 'prism' | false;
19
+ shikiConfig: ShikiConfig;
20
+ remarkPlugins: RemarkPlugins;
21
+ rehypePlugins: RehypePlugins;
10
22
  }
11
- export interface MarkdownRenderingOptions extends Partial<AstroMarkdownOptions> {
23
+ export interface MarkdownRenderingOptions extends AstroMarkdownOptions {
12
24
  /** @internal */
13
25
  $?: {
14
26
  scopedClassName: string | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/markdown-remark",
3
- "version": "0.7.0",
3
+ "version": "0.8.2",
4
4
  "type": "module",
5
5
  "author": "withastro",
6
6
  "license": "MIT",
@@ -15,11 +15,17 @@
15
15
  "exports": {
16
16
  ".": "./dist/index.js"
17
17
  },
18
+ "scripts": {
19
+ "prepublish": "pnpm build",
20
+ "build": "astro-scripts build \"src/**/*.ts\" && tsc -p tsconfig.json",
21
+ "build:ci": "astro-scripts build \"src/**/*.ts\"",
22
+ "postbuild": "astro-scripts copy \"src/**/*.js\"",
23
+ "dev": "astro-scripts dev \"src/**/*.ts\""
24
+ },
18
25
  "dependencies": {
19
26
  "@astrojs/prism": "^0.4.1",
20
27
  "assert": "^2.0.0",
21
28
  "github-slugger": "^1.4.0",
22
- "gray-matter": "^4.0.3",
23
29
  "mdast-util-mdx-expression": "^1.2.0",
24
30
  "mdast-util-mdx-jsx": "^1.2.0",
25
31
  "mdast-util-to-string": "^3.1.0",
@@ -40,14 +46,10 @@
40
46
  },
41
47
  "devDependencies": {
42
48
  "@types/github-slugger": "^1.3.0",
49
+ "@types/hast": "^2.3.4",
50
+ "@types/mdast": "^3.0.10",
43
51
  "@types/prismjs": "^1.26.0",
44
- "astro-scripts": "0.0.2"
45
- },
46
- "scripts": {
47
- "prepublish": "pnpm build",
48
- "build": "astro-scripts build \"src/**/*.ts\" && tsc -p tsconfig.json",
49
- "build:ci": "astro-scripts build \"src/**/*.ts\"",
50
- "postbuild": "astro-scripts copy \"src/**/*.js\"",
51
- "dev": "astro-scripts dev \"src/**/*.ts\""
52
+ "@types/unist": "^2.0.6",
53
+ "astro-scripts": "workspace:*"
52
54
  }
53
- }
55
+ }
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { AstroMarkdownOptions, MarkdownRenderingOptions } from './types';
1
+ import type { MarkdownRenderingOptions } from './types';
2
2
 
3
3
  import createCollectHeaders from './rehype-collect-headers.js';
4
4
  import scopedStyles from './remark-scoped-styles.js';
@@ -18,28 +18,17 @@ import markdown from 'remark-parse';
18
18
  import markdownToHtml from 'remark-rehype';
19
19
  import rehypeStringify from 'rehype-stringify';
20
20
  import rehypeRaw from 'rehype-raw';
21
- import matter from 'gray-matter';
22
21
 
23
- export { AstroMarkdownOptions, MarkdownRenderingOptions };
24
-
25
- /** Internal utility for rendering a full markdown file and extracting Frontmatter data */
26
- export async function renderMarkdownWithFrontmatter(contents: string, opts?: MarkdownRenderingOptions | null) {
27
- const { data: frontmatter, content } = matter(contents);
28
- const value = await renderMarkdown(content, opts);
29
- return { ...value, frontmatter };
30
- }
22
+ export * from './types.js';
31
23
 
32
24
  export const DEFAULT_REMARK_PLUGINS = ['remark-gfm', 'remark-smartypants'];
33
25
 
34
26
  export const DEFAULT_REHYPE_PLUGINS = ['rehype-slug'];
35
27
 
36
28
  /** Shared utility for rendering markdown */
37
- export async function renderMarkdown(content: string, opts?: MarkdownRenderingOptions | null) {
38
- let { remarkPlugins = [], rehypePlugins = [] } = opts ?? {};
39
- const scopedClassName = opts?.$?.scopedClassName;
40
- const mode = opts?.mode ?? 'mdx';
41
- const syntaxHighlight = opts?.syntaxHighlight ?? 'shiki';
42
- const shikiConfig = opts?.shikiConfig ?? {};
29
+ export async function renderMarkdown(content: string, opts: MarkdownRenderingOptions) {
30
+ let { mode, syntaxHighlight, shikiConfig, remarkPlugins, rehypePlugins } = opts;
31
+ const scopedClassName = opts.$?.scopedClassName;
43
32
  const isMDX = mode === 'mdx';
44
33
  const { headers, rehypeCollectHeaders } = createCollectHeaders();
45
34
 
@@ -47,8 +36,7 @@ export async function renderMarkdown(content: string, opts?: MarkdownRenderingOp
47
36
 
48
37
  let parser = unified()
49
38
  .use(markdown)
50
- .use(isMDX ? [remarkJsx] : [])
51
- .use(isMDX ? [remarkExpressions] : [])
39
+ .use(isMDX ? [remarkJsx, remarkExpressions] : [])
52
40
  .use([remarkUnwrap]);
53
41
 
54
42
  if (remarkPlugins.length === 0 && rehypePlugins.length === 0) {
@@ -73,22 +61,31 @@ export async function renderMarkdown(content: string, opts?: MarkdownRenderingOp
73
61
  parser.use([remarkPrism(scopedClassName)]);
74
62
  }
75
63
 
76
- parser.use([[markdownToHtml as any, { allowDangerousHtml: true, passThrough: ['raw', 'mdxTextExpression', 'mdxJsxTextElement', 'mdxJsxFlowElement'] }]]);
64
+ parser.use([
65
+ [
66
+ markdownToHtml as any,
67
+ {
68
+ allowDangerousHtml: true,
69
+ passThrough: ['raw', 'mdxTextExpression', 'mdxJsxTextElement', 'mdxJsxFlowElement'],
70
+ },
71
+ ],
72
+ ]);
77
73
 
78
74
  loadedRehypePlugins.forEach(([plugin, opts]) => {
79
75
  parser.use([[plugin, opts]]);
80
76
  });
81
77
 
82
78
  parser
83
- .use(isMDX ? [rehypeJsx] : [])
84
- .use(isMDX ? [rehypeExpressions] : [])
85
- .use(isMDX ? [] : [rehypeRaw])
86
- .use(isMDX ? [rehypeEscape] : [])
79
+ .use(isMDX ? [rehypeJsx, rehypeExpressions] : [rehypeRaw])
80
+ .use(rehypeEscape)
87
81
  .use(rehypeIslands);
88
82
 
89
83
  let result: string;
90
84
  try {
91
- const vfile = await parser.use([rehypeCollectHeaders]).use(rehypeStringify, { allowDangerousHtml: true }).process(content);
85
+ const vfile = await parser
86
+ .use([rehypeCollectHeaders])
87
+ .use(rehypeStringify, { allowDangerousHtml: true })
88
+ .process(content);
92
89
  result = vfile.toString();
93
90
  } catch (err) {
94
91
  console.error(err);
@@ -100,5 +97,3 @@ export async function renderMarkdown(content: string, opts?: MarkdownRenderingOp
100
97
  code: result.toString(),
101
98
  };
102
99
  }
103
-
104
- export default renderMarkdownWithFrontmatter;
@@ -1,5 +1,4 @@
1
1
  import * as unified from 'unified';
2
- import type { Plugin } from './types';
3
2
 
4
3
  async function importPlugin(p: string | unified.Plugin): Promise<unified.Plugin> {
5
4
  if (typeof p === 'string') {
@@ -10,7 +9,9 @@ async function importPlugin(p: string | unified.Plugin): Promise<unified.Plugin>
10
9
  return p;
11
10
  }
12
11
 
13
- export function loadPlugins(items: Plugin[]): Promise<[unified.Plugin, any?]>[] {
12
+ export function loadPlugins(
13
+ items: (string | [string, any] | unified.Plugin<any[], any> | [unified.Plugin<any[], any>, any])[]
14
+ ): Promise<[unified.Plugin, any?]>[] {
14
15
  return items.map((p) => {
15
16
  return new Promise((resolve, reject) => {
16
17
  if (Array.isArray(p)) {
@@ -2,7 +2,11 @@ import { SKIP, visit as _visit } from 'unist-util-visit';
2
2
 
3
3
  // This is a workaround.
4
4
  // It fixes a compatibility issue between different, incompatible ASTs given by plugins to Unist
5
- const visit = _visit as (node: any, type: string, callback?: (node: any, index: number, parent: any) => any) => any;
5
+ const visit = _visit as (
6
+ node: any,
7
+ type: string,
8
+ callback?: (node: any, index: number, parent: any) => any
9
+ ) => any;
6
10
 
7
11
  // This fixes some confusing bugs coming from somewhere inside of our Markdown pipeline.
8
12
  // `unist`/`remark`/`rehype` (not sure) often generate malformed HTML inside of <astro-root>
@@ -18,7 +22,11 @@ export default function rehypeIslands(): any {
18
22
  // Sometimes comments can be trapped as text, which causes them to be escaped
19
23
  // This casts them back to real HTML comments
20
24
  if (parent && child.value.indexOf('<!--') > -1 && index != null) {
21
- parent.children.splice(index, 1, { ...child, type: 'comment', value: child.value.replace('<!--', '').replace('-->', '').trim() });
25
+ parent.children.splice(index, 1, {
26
+ ...child,
27
+ type: 'comment',
28
+ value: child.value.replace('<!--', '').replace('-->', '').trim(),
29
+ });
22
30
  return [SKIP, index];
23
31
  }
24
32
  // For some reason `rehype` likes to inject extra linebreaks,
@@ -56,7 +56,9 @@ function transformer(className: MaybeString) {
56
56
  if (className) {
57
57
  classes.push(className);
58
58
  }
59
- node.value = `<pre class="${classes.join(' ')}"><code is:raw class="${classLanguage}">${html}</code></pre>`;
59
+ node.value = `<pre class="${classes.join(
60
+ ' '
61
+ )}"><code is:raw class="${classLanguage}">${html}</code></pre>`;
60
62
  return node;
61
63
  };
62
64
  return visit(tree, 'code', visitor);
@@ -1,34 +1,7 @@
1
1
  import type * as shiki from 'shiki';
2
2
  import { getHighlighter } from 'shiki';
3
3
  import { visit } from 'unist-util-visit';
4
-
5
- export interface ShikiConfig {
6
- /**
7
- * The languages loaded to Shiki.
8
- * Supports all languages listed here: https://github.com/shikijs/shiki/blob/main/docs/languages.md#all-languages
9
- * Instructions for loading a custom language: https://github.com/shikijs/shiki/blob/main/docs/languages.md#supporting-your-own-languages-with-shiki
10
- *
11
- * @default []
12
- */
13
- langs?: shiki.ILanguageRegistration[];
14
- /**
15
- * The styling theme.
16
- * Supports all themes listed here: https://github.com/shikijs/shiki/blob/main/docs/themes.md#all-themes
17
- * Instructions for loading a custom theme: https://github.com/shikijs/shiki/blob/main/docs/themes.md#loading-theme
18
- *
19
- * @default "github-dark"
20
- */
21
- theme?: shiki.IThemeRegistration;
22
- /**
23
- * Enable word wrapping.
24
- * - true: enabled.
25
- * - false: enabled.
26
- * - null: All overflow styling removed. Code will overflow the element by default.
27
- *
28
- * @default false
29
- */
30
- wrap?: boolean | null;
31
- }
4
+ import type { ShikiConfig } from './types.js';
32
5
 
33
6
  /**
34
7
  * getHighlighter() is the most expensive step of Shiki. Instead of calling it on every page,
@@ -37,7 +10,10 @@ export interface ShikiConfig {
37
10
  */
38
11
  const highlighterCacheAsync = new Map<string, Promise<shiki.Highlighter>>();
39
12
 
40
- const remarkShiki = async ({ langs = [], theme = 'github-dark', wrap = false }: ShikiConfig, scopedClassName?: string | null) => {
13
+ const remarkShiki = async (
14
+ { langs, theme, wrap }: ShikiConfig,
15
+ scopedClassName?: string | null
16
+ ) => {
41
17
  const cacheID: string = typeof theme === 'string' ? theme : theme.name;
42
18
  let highlighterAsync = highlighterCacheAsync.get(cacheID);
43
19
  if (!highlighterAsync) {
@@ -63,15 +39,24 @@ const remarkShiki = async ({ langs = [], theme = 'github-dark', wrap = false }:
63
39
  // &lt;span class=&quot;line&quot;
64
40
 
65
41
  // Replace "shiki" class naming with "astro" and add "is:raw".
66
- html = html.replace('<pre class="shiki"', `<pre is:raw class="astro-code${scopedClassName ? ' ' + scopedClassName : ''}"`);
42
+ html = html.replace(
43
+ '<pre class="shiki"',
44
+ `<pre is:raw class="astro-code${scopedClassName ? ' ' + scopedClassName : ''}"`
45
+ );
67
46
  // Replace "shiki" css variable naming with "astro".
68
- html = html.replace(/style="(background-)?color: var\(--shiki-/g, 'style="$1color: var(--astro-code-');
47
+ html = html.replace(
48
+ /style="(background-)?color: var\(--shiki-/g,
49
+ 'style="$1color: var(--astro-code-'
50
+ );
69
51
  // Handle code wrapping
70
52
  // if wrap=null, do nothing.
71
53
  if (wrap === false) {
72
54
  html = html.replace(/style="(.*?)"/, 'style="$1; overflow-x: auto;"');
73
55
  } else if (wrap === true) {
74
- html = html.replace(/style="(.*?)"/, 'style="$1; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;"');
56
+ html = html.replace(
57
+ /style="(.*?)"/,
58
+ 'style="$1; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;"'
59
+ );
75
60
  }
76
61
 
77
62
  // Apply scopedClassName to all nested lines
@@ -2,7 +2,11 @@ import { visit as _visit, SKIP } from 'unist-util-visit';
2
2
 
3
3
  // This is a workaround.
4
4
  // It fixes a compatibility issue between different, incompatible ASTs given by plugins to Unist
5
- const visit = _visit as (node: any, type: string, callback?: (node: any, index: number, parent: any) => any) => any;
5
+ const visit = _visit as (
6
+ node: any,
7
+ type: string,
8
+ callback?: (node: any, index: number, parent: any) => any
9
+ ) => any;
6
10
 
7
11
  // Remove the wrapping paragraph for <astro-root> islands
8
12
  export default function remarkUnwrap() {
@@ -33,6 +37,8 @@ export default function remarkUnwrap() {
33
37
  };
34
38
 
35
39
  function containsAstroRootNode(node: any) {
36
- return node.children.map((child: any) => astroRootNodes.has(child)).reduce((all: boolean, v: boolean) => (all ? all : v), false);
40
+ return node.children
41
+ .map((child: any) => astroRootNodes.has(child))
42
+ .reduce((all: boolean, v: boolean) => (all ? all : v), false);
37
43
  }
38
44
  }
package/src/types.ts CHANGED
@@ -1,17 +1,40 @@
1
1
  import type * as unified from 'unified';
2
- import type { ShikiConfig } from './remark-shiki';
2
+ import type * as mdast from 'mdast';
3
+ import type * as hast from 'hast';
4
+ import type { ILanguageRegistration, IThemeRegistration, Theme } from 'shiki';
3
5
 
4
- export type Plugin = string | [string, any] | unified.Plugin | [unified.Plugin, any];
6
+ export type { Node } from 'unist';
7
+
8
+ export type RemarkPlugin<PluginParameters extends any[] = any[]> = unified.Plugin<
9
+ PluginParameters,
10
+ mdast.Root
11
+ >;
12
+
13
+ export type RemarkPlugins = (string | [string, any] | RemarkPlugin | [RemarkPlugin, any])[];
14
+
15
+ export type RehypePlugin<PluginParameters extends any[] = any[]> = unified.Plugin<
16
+ PluginParameters,
17
+ hast.Root
18
+ >;
19
+
20
+ export type RehypePlugins = (string | [string, any] | RehypePlugin | [RehypePlugin, any])[];
21
+
22
+ export interface ShikiConfig {
23
+ langs: ILanguageRegistration[];
24
+ theme: Theme | IThemeRegistration;
25
+ wrap: boolean | null;
26
+ }
5
27
 
6
28
  export interface AstroMarkdownOptions {
7
- mode?: 'md' | 'mdx';
8
- syntaxHighlight?: 'shiki' | 'prism' | false;
9
- shikiConfig?: ShikiConfig;
10
- remarkPlugins?: Plugin[];
11
- rehypePlugins?: Plugin[];
29
+ mode: 'md' | 'mdx';
30
+ drafts: boolean;
31
+ syntaxHighlight: 'shiki' | 'prism' | false;
32
+ shikiConfig: ShikiConfig;
33
+ remarkPlugins: RemarkPlugins;
34
+ rehypePlugins: RehypePlugins;
12
35
  }
13
36
 
14
- export interface MarkdownRenderingOptions extends Partial<AstroMarkdownOptions> {
37
+ export interface MarkdownRenderingOptions extends AstroMarkdownOptions {
15
38
  /** @internal */
16
39
  $?: {
17
40
  scopedClassName: string | null;
package/LICENSE DELETED
@@ -1,34 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2021 Fred K. Schott
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
22
-
23
-
24
- """
25
- This license applies to parts of the `packages/create-astro` and `packages/astro` subdirectories originating from the https://github.com/sveltejs/kit repository:
26
-
27
- Copyright (c) 2020 [these people](https://github.com/sveltejs/kit/graphs/contributors)
28
-
29
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
30
-
31
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
32
-
33
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34
- """