@md-plugins/md-plugin-frontmatter 0.1.0-alpha.28 → 0.1.0-alpha.29

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/dist/index.d.mts CHANGED
@@ -44,9 +44,8 @@ declare module '@md-plugins/shared' {
44
44
  }
45
45
 
46
46
  /**
47
- * Get markdown frontmatter and excerpt
48
- *
49
- * Extract them into env
47
+ * Get markdown frontmatter and excerpt.
48
+ * Extract them into env.
50
49
  */
51
50
  declare const frontmatterPlugin: PluginWithOptions<FrontmatterPluginOptions>;
52
51
 
package/dist/index.d.ts CHANGED
@@ -44,9 +44,8 @@ declare module '@md-plugins/shared' {
44
44
  }
45
45
 
46
46
  /**
47
- * Get markdown frontmatter and excerpt
48
- *
49
- * Extract them into env
47
+ * Get markdown frontmatter and excerpt.
48
+ * Extract them into env.
50
49
  */
51
50
  declare const frontmatterPlugin: PluginWithOptions<FrontmatterPluginOptions>;
52
51
 
package/dist/index.mjs CHANGED
@@ -1,6 +1,26 @@
1
1
  import grayMatter from 'gray-matter';
2
2
 
3
- const frontmatterPlugin = (md, { grayMatterOptions, renderExcerpt = false } = {}) => {
3
+ function resolvePluginOptions(options, key, defaults) {
4
+ if (options && typeof options === "object" && key in options) {
5
+ return { ...defaults, ...options[key] };
6
+ }
7
+ return { ...defaults, ...options };
8
+ }
9
+
10
+ const DEFAULT_FRONTMATTER_PLUGIN_OPTIONS = {
11
+ grayMatterOptions: {},
12
+ renderExcerpt: false
13
+ };
14
+ const frontmatterPlugin = (md, options) => {
15
+ const resolvedOptions = resolvePluginOptions(
16
+ options,
17
+ "frontmatterPlugin",
18
+ DEFAULT_FRONTMATTER_PLUGIN_OPTIONS
19
+ );
20
+ const {
21
+ grayMatterOptions = DEFAULT_FRONTMATTER_PLUGIN_OPTIONS.grayMatterOptions,
22
+ renderExcerpt = DEFAULT_FRONTMATTER_PLUGIN_OPTIONS.renderExcerpt
23
+ } = resolvedOptions;
4
24
  const render = md.render.bind(md);
5
25
  md.render = (src, env = {}) => {
6
26
  let data, content, excerpt;
@@ -15,17 +35,10 @@ const frontmatterPlugin = (md, { grayMatterOptions, renderExcerpt = false } = {}
15
35
  }
16
36
  env.content = content;
17
37
  env.frontmatter = {
18
- // allow providing default value
19
38
  ...env.frontmatter,
20
39
  ...data
21
40
  };
22
- env.excerpt = renderExcerpt && data.excerpt ? (
23
- // render the excerpt with original markdown-it render method.
24
- render(data.excerpt, env)
25
- ) : (
26
- // use the raw excerpt directly
27
- excerpt
28
- );
41
+ env.excerpt = renderExcerpt && data.excerpt ? render(data.excerpt, env) : excerpt;
29
42
  return render(content, env);
30
43
  };
31
44
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@md-plugins/md-plugin-frontmatter",
3
- "version": "0.1.0-alpha.28",
3
+ "version": "0.1.0-alpha.29",
4
4
  "description": "A markdown-it plugin for handling frontmatter in markdown files.",
5
5
  "keywords": [
6
6
  "markdown-it",
@@ -36,7 +36,7 @@
36
36
  "gray-matter": "^4.0.3",
37
37
  "markdown-it": "^14.1.0",
38
38
  "@types/markdown-it": "^14.1.2",
39
- "@md-plugins/shared": "0.1.0-alpha.28"
39
+ "@md-plugins/shared": "0.1.0-alpha.29"
40
40
  },
41
41
  "peerDependencies": {
42
42
  "gray-matter": "^4.0.3",