mikel-markdown 0.25.1 → 0.26.1

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 (3) hide show
  1. package/README.md +12 -0
  2. package/index.js +23 -14
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -55,6 +55,18 @@ And finally, compile your template:
55
55
  const result = m({}); // --> '<p>Hello <strong>world</strong></p>'
56
56
  ```
57
57
 
58
+ ### Embedded HTML Blocks
59
+
60
+ By default, `mikel-markdown` transforms most content to ensure consistent Markdown rendering, including HTML blocks. However, if you need to include raw HTML blocks, such as `<div>`, `<section>`, or custom components, you can wrap your HTML block between `<!--html-->` and `<!--/html-->` like so:
61
+
62
+ ```html
63
+ <!--html-->
64
+ <div class="center">
65
+ <p>This content will be preserved as raw HTML.</p>
66
+ </div>
67
+ <!--/html-->
68
+ ```
69
+
58
70
  ## Helpers
59
71
 
60
72
  ### `#markdown`
package/index.js CHANGED
@@ -80,14 +80,14 @@ const allExpressions = {
80
80
  replace: (args, cn) => {
81
81
  return render("ul", {class: cn.list}, render("li", {class: cn.listItem}, args[1]));
82
82
  },
83
- afterRegex: /(<\/ul>\n(?:.*)<ul ?(?:class=".*")?>*)+/g
83
+ afterRegex: /(<\/ul>\n(?:.*)<ul ?(?:class="[^"]*")?>*)+/g
84
84
  },
85
85
  orderedList: {
86
86
  regex: /^[\t\s]*?(?:\d(?:\)|\.))\s(.*)/gm,
87
87
  replace: (args, cn) => {
88
88
  return render("ol", {class: cn.list}, render("li", {class: cn.listItem}, args[1]));
89
89
  },
90
- afterRegex: /(<\/ol>\n(?:.*)<ol ?(?:class=".*")?>*)+/g
90
+ afterRegex: /(<\/ol>\n(?:.*)<ol ?(?:class="[^"]*")?>*)+/g
91
91
  },
92
92
  strong: {
93
93
  regex: /(?:\*\*|__)([^\n]+?)(?:\*\*|__)/g,
@@ -101,8 +101,8 @@ const allExpressions = {
101
101
  regex: /^((?:.+(?:\n|$))+)/gm,
102
102
  replace: (args, cn) => {
103
103
  const line = args[0].trim();
104
- // Check if the line starts with a block tag
105
- if (/^\<\/?(ul|ol|bl|h\d|p|div|sty|scr).*/.test(line.slice(0, 4)) === true) {
104
+ // check if the line starts with a block tag or is an empty line
105
+ if (!line || /^\<(\/? *(ul|ol|bl|h\d|p|div|sty|scr|t)|!--)/.test(line.slice(0, 4))) {
106
106
  return line;
107
107
  }
108
108
  return render("p", {class: cn.paragraph}, line.replace(/\n/g, ""));
@@ -110,10 +110,13 @@ const allExpressions = {
110
110
  }
111
111
  };
112
112
 
113
- // @description inline expressions
114
- const inlineExpressions = Object.fromEntries(["code", "link", "strong", "emphasis"].map(key => {
115
- return [key, allExpressions[key]];
116
- }));
113
+ // @description get only inline expressions
114
+ const getInlineExpressions = expressions => {
115
+ const fields = ["code", "link", "strong", "emphasis", "image"].filter(key => !!expressions[key]);
116
+ return Object.fromEntries(fields.map(key => {
117
+ return [key, expressions[key]];
118
+ }));
119
+ };
117
120
 
118
121
  // @description markdown parser
119
122
  const parser = (str = "", options = {}) => {
@@ -121,13 +124,19 @@ const parser = (str = "", options = {}) => {
121
124
  const expressions = options?.expressions || allExpressions; // custom expressions
122
125
  const ignoredBlocks = []; // chunks to ignore
123
126
  str = str.replace(/\r\n/g, "\n");
127
+ // ignore html blocks
128
+ const htmlBlockRegex = /<!--html-->([\s\S]*?)<!--\/html-->/gm;
129
+ str = str.replace(htmlBlockRegex, match => {
130
+ ignoredBlocks.push(match);
131
+ return `<!--HTML-BLOCK-${(ignoredBlocks.length - 1)}-->`;
132
+ });
124
133
  // replace all expressions
125
134
  Object.keys(expressions).forEach(key => {
126
135
  str = str.replace(expressions[key].regex, (...args) => {
127
136
  const value = expressions[key].replace(args, classNames);
128
137
  if (key === "pre" || key === "code") {
129
138
  ignoredBlocks.push(value);
130
- return `<pre>{IGNORED%${(ignoredBlocks.length - 1)}}</pre>`;
139
+ return `<!--HTML-BLOCK-${(ignoredBlocks.length - 1)}-->`;
131
140
  }
132
141
  // other value --> return the value provided by the renderer
133
142
  return value;
@@ -137,10 +146,10 @@ const parser = (str = "", options = {}) => {
137
146
  str = str.replace(expressions[key].afterRegex, "");
138
147
  }
139
148
  });
140
- // Replace all the ignored blocks
141
- for (let i = ignoredBlocks.length - 1; i >= 0; i--) {
142
- str = str.replace(`<pre>{IGNORED%${i}}</pre>`, ignoredBlocks[i]);
143
- }
149
+ // replace all the ignored blocks
150
+ ignoredBlocks.forEach((block, index) => {
151
+ str = str.replace(`<!--HTML-BLOCK-${index}-->`, block);
152
+ });
144
153
  return str;
145
154
  };
146
155
 
@@ -154,8 +163,8 @@ const markdownPlugin = (options = {}) => {
154
163
  },
155
164
  inlineMarkdown: params => {
156
165
  return parser(params.fn(params.data) || "", {
157
- expressions: inlineExpressions,
158
166
  ...options,
167
+ expressions: getInlineExpressions(options.expressions || allExpressions),
159
168
  });
160
169
  },
161
170
  },
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.25.1",
4
+ "version": "0.26.1",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "author": {