mikel-markdown 0.19.0 → 0.20.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 (3) hide show
  1. package/README.md +19 -1
  2. package/index.js +32 -13
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -52,7 +52,25 @@ m.use(mikelMarkdown());
52
52
 
53
53
  And finally, compile your template:
54
54
  ```javascript
55
- const result = m({}); // --> 'Hello <strong>world</strong>'
55
+ const result = m({}); // --> '<p>Hello <strong>world</strong></p>'
56
+ ```
57
+
58
+ ## Helpers
59
+
60
+ ### `#markdown`
61
+
62
+ The `#markdown` helper is used to convert Markdown content into HTML markup. It takes the content inside the helper and processes it using a Markdown parser, returning the resulting HTML.
63
+
64
+ ```javascript
65
+ m(`{{#markdown}}Hello **world**{{/markdown}}`); // --> '<p>Hello <strong>world</strong></p>'
66
+ ```
67
+
68
+ ### `#inlineMarkdown`
69
+
70
+ The `#inlineMarkdown` helper is used to convert inline Markdown content into HTML markup. This helper is useful for rendering Markdown content that is not block-level, such as inline code, emphasis, or links.
71
+
72
+ ```javascript
73
+ m(`{{#inlineMarkdown}}Hello **world**{{/inlineMarkdown}}`); // --> 'Hello <strong>world</strong>'
56
74
  ```
57
75
 
58
76
  ## License
package/index.js CHANGED
@@ -1,6 +1,19 @@
1
- const escape = str => encodeURIComponent(str);
1
+ const escapedChars = {
2
+ "&": "&amp;",
3
+ "<": "&lt;",
4
+ ">": "&gt;",
5
+ '"': "&quot;",
6
+ "'": "&#039;",
7
+ };
8
+
9
+ const escape = s => s.toString().replace(/[&<>\"']/g, m => escapedChars[m]);
2
10
 
3
11
  // @description custom method to render the provided tag and content
12
+ // @param tag {string} tag to render
13
+ // @param props {object} attributes to add to the tag
14
+ // @param content {string} content to add to the tag
15
+ // @return {string} the rendered tag
16
+ // @example render("div", {class: "my-class"}, "Hello world") --> '<div class="my-class">Hello world</div>'
4
17
  const render = (tag, props = {}, content = "") => {
5
18
  const attrs = Object.keys(props).filter(k => !!props[k]).map(k => `${k}="${props[k]}"`);
6
19
  if (tag === "hr" || tag === "img") {
@@ -9,7 +22,8 @@ const render = (tag, props = {}, content = "") => {
9
22
  return `<${[tag, ...attrs].join(" ")}>${content}</${tag}>`;
10
23
  };
11
24
 
12
- const expressions = {
25
+ // @description all available expressions
26
+ const allExpressions = {
13
27
  pre: {
14
28
  regex: /(?:^```(?:[^\n]*)\n([\s\S]*?)\n``` *$)/gm,
15
29
  replace: (args, cn) => render("pre", {class: cn.pre}, escape(args[1])),
@@ -27,7 +41,7 @@ const expressions = {
27
41
  replace: (args, cn) => render("code", {class: cn.code}, escape(args[1])),
28
42
  },
29
43
  image: {
30
- regex: /!\[([^\]]*?)\]\(([^)]*?)\)/g,
44
+ regex: /\!\[([^\]]*?)\]\(([^)]*?)\)/g,
31
45
  replace: (args, cn) => render("img", {class: cn.image, alt: args[1], src: args[2]}),
32
46
  },
33
47
  table: {
@@ -96,15 +110,17 @@ const expressions = {
96
110
  }
97
111
  };
98
112
 
113
+ // @description inline expressions
114
+ const inlineExpressions = Object.fromEntries(["link", "strong", "emphasis", "code"].map(key => {
115
+ return [key, allExpressions[key]];
116
+ }));
117
+
99
118
  // @description markdown parser
100
- const parser = (str, options = {}) => {
119
+ const parser = (str = "", options = {}) => {
101
120
  const classNames = options?.classNames || {}; // custom classNames
121
+ const expressions = options?.expressions || allExpressions; // custom expressions
102
122
  const ignoredBlocks = []; // chunks to ignore
103
123
  str = str.replace(/\r\n/g, "\n");
104
- // replace all <script> tags
105
- // str = str.replace(/<script[^\0]*?>([^\0]*?)<\/script>/gmi, function (match, content) {
106
- // return "&lt;script&gt;" + content + "&lt;/script&gt;";
107
- // });
108
124
  // replace all expressions
109
125
  Object.keys(expressions).forEach(key => {
110
126
  str = str.replace(expressions[key].regex, (...args) => {
@@ -121,10 +137,6 @@ const parser = (str, options = {}) => {
121
137
  str = str.replace(expressions[key].afterRegex, "");
122
138
  }
123
139
  });
124
- // Replace all line breaks expressions
125
- // str = str.replace(/^\n\n+/gm, function () {
126
- // return renderer("br", {});
127
- // });
128
140
  // Replace all the ignored blocks
129
141
  for (let i = ignoredBlocks.length - 1; i >= 0; i--) {
130
142
  str = str.replace(`<pre>{IGNORED%${i}}</pre>`, ignoredBlocks[i]);
@@ -140,13 +152,20 @@ const markdownPlugin = (options = {}) => {
140
152
  markdown: params => {
141
153
  return parser(params.fn(params.data) || "", options);
142
154
  },
155
+ inlineMarkdown: params => {
156
+ return parser(params.fn(params.data) || "", {
157
+ expressions: inlineExpressions,
158
+ ...options,
159
+ });
160
+ },
143
161
  },
144
162
  };
145
163
  };
146
164
 
147
165
  // assign additional options for this plugin
148
166
  markdownPlugin.parser = parser;
149
- markdownPlugin.expressions = expressions;
167
+ markdownPlugin.render = render;
168
+ markdownPlugin.expressions = allExpressions;
150
169
 
151
170
  // export the plugin
152
171
  export default markdownPlugin;
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.19.0",
4
+ "version": "0.20.0",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "author": {