mikel-markdown 0.28.0 → 0.29.0

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.
Files changed (2) hide show
  1. package/index.js +54 -4
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -41,8 +41,15 @@ const allExpressions = {
41
41
  regex: /^(#+)\s+(.*)/gm,
42
42
  replace: (args, opt) => {
43
43
  const level = args[1].length;
44
- const cn = [opt.classNames?.heading, opt.classNames?.["heading" + level]];
45
- return render("h" + level, {class: cn.filter(Boolean).join(" ")}, args[2]);
44
+ const headingClassNames = [
45
+ opt.classNames?.heading,
46
+ opt.classNames?.["heading" + level]
47
+ ];
48
+ const headingProps = {
49
+ class: headingClassNames.filter(Boolean).join(" "),
50
+ name: args[2].toLowerCase().replaceAll(" ", "-"),
51
+ };
52
+ return render("h" + level, headingProps, args[2]);
46
53
  },
47
54
  },
48
55
  blockquote: {
@@ -141,8 +148,13 @@ const getInlineExpressions = expressions => {
141
148
  // @description markdown parser
142
149
  const parser = (str = "", options = {}) => {
143
150
  const expressions = options?.expressions || allExpressions; // custom expressions
151
+ const hooks = options?.hooks || {};
144
152
  const ignoredBlocks = []; // chunks to ignore
145
153
  str = str.replace(/\r\n/g, "\n");
154
+ // call preprocess hook
155
+ if (typeof hooks?.preprocess === "function") {
156
+ str = hooks.preprocess(str, options);
157
+ }
146
158
  // ignore html blocks
147
159
  const htmlBlockRegex = /<!--html-->([\s\S]*?)<!--\/html-->/gm;
148
160
  str = str.replace(htmlBlockRegex, match => {
@@ -152,7 +164,22 @@ const parser = (str = "", options = {}) => {
152
164
  // replace all expressions
153
165
  Object.keys(expressions).forEach(key => {
154
166
  str = str.replace(expressions[key].regex, (...args) => {
155
- const value = expressions[key].replace(args, options);
167
+ // call the before render hook
168
+ if (typeof hooks?.beforeRender === "function") {
169
+ const newArgs = hooks.beforeRender(key, args, options);
170
+ if (newArgs && Array.isArray(newArgs)) {
171
+ args = newArgs;
172
+ }
173
+ }
174
+ // get the result
175
+ let value = expressions[key].replace(args, options);
176
+ // call the after render hook
177
+ if (typeof hooks?.afterRender === "function") {
178
+ let newValue = hooks.afterRender(value, key, args, options);
179
+ if (typeof newValue === "string" && newValue !== value) {
180
+ value = newValue;
181
+ }
182
+ }
156
183
  if (key === "pre" || key === "code") {
157
184
  ignoredBlocks.push(value);
158
185
  return `<!--HTML-BLOCK-${(ignoredBlocks.length - 1)}-->`;
@@ -169,6 +196,10 @@ const parser = (str = "", options = {}) => {
169
196
  ignoredBlocks.forEach((block, index) => {
170
197
  str = str.replace(`<!--HTML-BLOCK-${index}-->`, block);
171
198
  });
199
+ // call postprocess hook
200
+ if (typeof hooks?.postprocess === "function") {
201
+ str = hooks.postprocess(str, options);
202
+ }
172
203
  return str;
173
204
  };
174
205
 
@@ -178,7 +209,26 @@ const markdownPlugin = (options = {}) => {
178
209
  return {
179
210
  helpers: {
180
211
  markdown: params => {
181
- return parser(params.fn(params.data) || "", options);
212
+ const tableOfContents = [];
213
+ const result = parser(params.fn(params.data) || "", {
214
+ ...options,
215
+ hooks: {
216
+ beforeRender: (key, args) => {
217
+ if (key === "heading") {
218
+ tableOfContents.push({
219
+ level: args[1].length,
220
+ text: args[2],
221
+ slug: args[2].toLowerCase().replaceAll(" ", "-"),
222
+ });
223
+ }
224
+ },
225
+ },
226
+ });
227
+ // create a new variable @toc with the generated table of contents
228
+ Object.assign(params.variables, {
229
+ "toc": tableOfContents,
230
+ });
231
+ return result;
182
232
  },
183
233
  inlineMarkdown: params => {
184
234
  return parser(params.fn(params.data) || "", {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mikel-markdown",
3
3
  "description": "A mikel helper for parsing markdown content.",
4
- "version": "0.28.0",
4
+ "version": "0.29.0",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "author": {