zenn-markdown-html 0.1.121 → 0.1.122-alpha.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/README.md CHANGED
@@ -1,17 +1,34 @@
1
1
  # Zenn Markdown Html
2
2
 
3
- マークダウンを HTML へ変換するためのライブラリ。
3
+ マークダウンを HTML へ変換するためのライブラリです。
4
4
 
5
- ## 使い方
5
+ ## API
6
+
7
+ ### markdownToHtml
8
+
9
+ MarkdownをHTMLに変換します。
6
10
 
7
11
  ```js
8
12
  import markdownToHtml from 'zenn-markdown-html';
9
13
  const html = markdownToHtml(markdown);
10
14
  ```
11
15
 
12
- ## 使用できる Markdown の記法について
16
+ サポートする記法については、[Zenn Markdown 記法一覧](https://zenn.dev/zenn/articles/markdown-guide) を参照してください。
17
+
18
+ ### markdownToSimpleHtml
19
+
20
+ MarkdownをHTMLに変換します。markdownToHtmlと比べてサポートする記法が限られています。
21
+
22
+ ```js
23
+ import { markdownToSimpleHtml } from 'zenn-markdown-html';
24
+ const html = markdownToSimpleHtml(markdown);
25
+ ```
26
+
27
+ サポートする記法
13
28
 
14
- - [Zenn の Markdown 記法一覧 | Zenn](https://zenn.dev/zenn/articles/markdown-guide)
29
+ - Markdown記法(リンク、リスト、太字・斜体)
30
+ - パラグラフ内の改行
31
+ - URLをリンクに変換
15
32
 
16
33
  ## 開発者向けのドキュメント
17
34
 
package/lib/index.d.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  declare const markdownToHtml: (text: string) => string;
2
2
  export default markdownToHtml;
3
+ import { markdownToSimpleHtml } from './markdown-to-simple-html';
4
+ export { markdownToSimpleHtml };
package/lib/index.js CHANGED
@@ -4,6 +4,12 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
+ Object.defineProperty(exports, "markdownToSimpleHtml", {
8
+ enumerable: true,
9
+ get: function () {
10
+ return _markdownToSimpleHtml.markdownToSimpleHtml;
11
+ }
12
+ });
7
13
 
8
14
  var _markdownIt = _interopRequireDefault(require("markdown-it"));
9
15
 
@@ -13,6 +19,8 @@ var _mdContainer = require("./utils/md-container");
13
19
 
14
20
  var _mdRendererFence = require("./utils/md-renderer-fence");
15
21
 
22
+ var _mdLinkAttributes = require("./utils/md-link-attributes");
23
+
16
24
  var _mdLinkifyToCard = require("./utils/md-linkify-to-card");
17
25
 
18
26
  var _mdKatex = require("./utils/md-katex");
@@ -25,6 +33,8 @@ var _markdownItImsize = _interopRequireDefault(require("@steelydylan/markdown-it
25
33
 
26
34
  var _markdownItAnchor = _interopRequireDefault(require("markdown-it-anchor"));
27
35
 
36
+ var _markdownToSimpleHtml = require("./markdown-to-simple-html");
37
+
28
38
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
39
 
30
40
  // plugis
@@ -36,8 +46,6 @@ const mdTaskLists = require('markdown-it-task-lists');
36
46
 
37
47
  const mdInlineComments = require('markdown-it-inline-comments');
38
48
 
39
- const mdLinkAttributes = require('markdown-it-link-attributes');
40
-
41
49
  const md = (0, _markdownIt.default)({
42
50
  breaks: true,
43
51
  linkify: true
@@ -47,20 +55,7 @@ md.linkify.set({
47
55
  });
48
56
  md.use(_mdBr.mdBr).use(_mdRendererFence.mdRendererFence).use(_markdownItImsize.default).use(_mdCustomBlock.mdCustomBlock).use(mdContainer, 'details', _mdContainer.containerDetailsOptions).use(mdContainer, 'message', _mdContainer.containerMessageOptions).use(mdFootnote).use(mdTaskLists, {
49
57
  enabled: true
50
- }).use(mdInlineComments).use(mdLinkAttributes, [// 内部リンク
51
- {
52
- pattern: /^(?:https:\/\/zenn\.dev$)|(?:https:\/\/zenn\.dev\/.*$)/,
53
- attrs: {
54
- target: '_blank'
55
- }
56
- }, // 外部リンク
57
- {
58
- pattern: /^https?:\/\//,
59
- attrs: {
60
- target: '_blank',
61
- rel: 'nofollow noopener noreferrer'
62
- }
63
- }]).use(_markdownItAnchor.default, {
58
+ }).use(mdInlineComments).use(_mdLinkAttributes.mdLinkAttributes).use(_markdownItAnchor.default, {
64
59
  level: [1, 2, 3, 4],
65
60
  permalink: _markdownItAnchor.default.permalink.ariaHidden({
66
61
  placement: 'before',
@@ -0,0 +1 @@
1
+ export declare const markdownToSimpleHtml: (text: string) => string;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.markdownToSimpleHtml = void 0;
7
+
8
+ var _markdownIt = _interopRequireDefault(require("markdown-it"));
9
+
10
+ var _mdLinkAttributes = require("./utils/md-link-attributes");
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+
14
+ // preset 'zero' はデフォルトで全ての変換を無効化したプリセットです。
15
+ // Ref: https://github.com/markdown-it/markdown-it/blob/master/lib/presets/zero.js
16
+ const md = (0, _markdownIt.default)('zero', {
17
+ breaks: true,
18
+ // 改行を<br>に変換する
19
+ linkify: true // URLをリンクに変換する
20
+
21
+ });
22
+ md.enable('emphasis'); // 太字と斜体を有効化
23
+
24
+ md.enable('link'); // リンクを有効化
25
+
26
+ md.enable('list'); // リストを有効化
27
+ // 改行コードを<br>に変換する('zero'presetの場合、newlineを有効化する必要がある)
28
+ // Ref: https://github.com/markdown-it/markdown-it/issues/491
29
+
30
+ md.enable('newline'); // linkify を有効化('zero'presetの場合、linkifyを有効化する必要がある)
31
+ // Ref: https://github.com/markdown-it/markdown-it/issues/396
32
+
33
+ md.enable('linkify'); // fuzzyLink - recognize URL-s without http(s):// head. Default true.
34
+ // Ref: http://markdown-it.github.io/linkify-it/doc/#LinkifyIt.prototype.set
35
+
36
+ md.linkify.set({
37
+ fuzzyLink: false
38
+ });
39
+ md.use(_mdLinkAttributes.mdLinkAttributes); // 限られた記法のみをHTMLに変換するパーサー
40
+
41
+ const markdownToSimpleHtml = text => {
42
+ if (!(text && text.length)) return '';
43
+ return md.render(text);
44
+ };
45
+
46
+ exports.markdownToSimpleHtml = markdownToSimpleHtml;
@@ -84,6 +84,16 @@ const blockOptions = {
84
84
  return (0, _helper.generateEmbedIframe)('tweet', str);
85
85
  },
86
86
 
87
+ blueprintue(str) {
88
+ if (!(0, _urlMatcher.isBlueprintUEUrl)(str)) return '「https://blueprintue.com/render/」から始まる正しいURLを指定してください';
89
+ return `<div class="embed-blueprintue"><iframe src="${str}" width="100%" height="500px" scrolling="no" frameborder="no" loading="lazy" allowfullscreen></iframe></div>`;
90
+ },
91
+
92
+ figma(str) {
93
+ if (!(0, _urlMatcher.isFigmaUrl)(str)) return 'ファイルまたはプロトタイプのFigma URLを指定してください';
94
+ return `<div class="embed-figma"><iframe src="https://www.figma.com/embed?embed_host=zenn&url=${str}" scrolling="no" frameborder="no" loading="lazy" allowfullscreen width="100%" height="500px"></iframe></div>`;
95
+ },
96
+
87
97
  card(str) {
88
98
  if (!(0, _helper.isValidHttpUrl)(str)) return 'URLが不正です';
89
99
  return (0, _helper.generateEmbedIframe)('link-card', str);
@@ -0,0 +1,2 @@
1
+ import MarkdownIt from 'markdown-it';
2
+ export declare function mdLinkAttributes(md: MarkdownIt): void;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.mdLinkAttributes = mdLinkAttributes;
7
+
8
+ const markdownItLinkAttributes = require('markdown-it-link-attributes');
9
+
10
+ function mdLinkAttributes(md) {
11
+ // <a>タグの属性を設定する
12
+ // Ref: https://github.com/crookedneighbor/markdown-it-link-attributes
13
+ md.use(markdownItLinkAttributes, [// 内部リンク
14
+ {
15
+ matcher(href) {
16
+ return href.match(/^(?:https:\/\/zenn\.dev$)|(?:https:\/\/zenn\.dev\/.*$)/);
17
+ },
18
+
19
+ attrs: {
20
+ target: '_blank'
21
+ }
22
+ }, // 外部リンク
23
+ {
24
+ matcher(href) {
25
+ return href.match(/^https?:\/\//);
26
+ },
27
+
28
+ attrs: {
29
+ target: '_blank',
30
+ rel: 'nofollow noopener noreferrer'
31
+ }
32
+ }]);
33
+ }
@@ -88,9 +88,21 @@ function mdLinkifyToCard(md) {
88
88
  md.core.ruler.after('replacements', 'link-to-card', function ({
89
89
  tokens
90
90
  }) {
91
- // 本文内のすべてのtokenをチェック
91
+ // 埋め込みを許可するネストレベル
92
+ let allowLevel = 0; // 本文内のすべてのtokenをチェック
93
+
92
94
  tokens.forEach((token, i) => {
93
- // autolinkはinline内のchildrenにのみ存在
95
+ if (token.type === 'container_details_open') {
96
+ allowLevel++;
97
+ return;
98
+ }
99
+
100
+ if (token.type === 'container_details_close' && allowLevel > 0) {
101
+ allowLevel--;
102
+ return;
103
+ } // autolinkはinline内のchildrenにのみ存在
104
+
105
+
94
106
  if (token.type !== 'inline') return; // childrenが存在しない場合は変換しない
95
107
 
96
108
  const children = token.children;
@@ -100,7 +112,7 @@ function mdLinkifyToCard(md) {
100
112
  if (!hasAnyAutolink) return; // 親がコンテンツ直下のp要素の場合のみ変換
101
113
 
102
114
  const parentToken = tokens[i - 1];
103
- const isParentRootParagraph = parentToken && parentToken.type === 'paragraph_open' && parentToken.level === 0;
115
+ const isParentRootParagraph = parentToken && parentToken.type === 'paragraph_open' && parentToken.level === allowLevel;
104
116
  if (!isParentRootParagraph) return;
105
117
  token.children = convertAutolinkToEmbed(children);
106
118
  });
@@ -9,3 +9,12 @@ export declare function extractYoutubeVideoParameters(youtubeUrl: string): {
9
9
  start?: string;
10
10
  } | undefined;
11
11
  export declare function isYoutubeUrl(url: string): boolean;
12
+ /**
13
+ * 参考: https://blueprintue.com/
14
+ * 生成されるURLをもとに正規表現を定義した
15
+ */
16
+ export declare function isBlueprintUEUrl(url: string): boolean;
17
+ /**
18
+ * 参考: https://www.figma.com/developers/embed
19
+ */
20
+ export declare function isFigmaUrl(url: string): boolean;
@@ -4,8 +4,10 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.extractYoutubeVideoParameters = extractYoutubeVideoParameters;
7
+ exports.isBlueprintUEUrl = isBlueprintUEUrl;
7
8
  exports.isCodepenUrl = isCodepenUrl;
8
9
  exports.isCodesandboxUrl = isCodesandboxUrl;
10
+ exports.isFigmaUrl = isFigmaUrl;
9
11
  exports.isGistUrl = isGistUrl;
10
12
  exports.isJsfiddleUrl = isJsfiddleUrl;
11
13
  exports.isStackblitzUrl = isStackblitzUrl;
@@ -57,4 +59,21 @@ function extractYoutubeVideoParameters(youtubeUrl) {
57
59
 
58
60
  function isYoutubeUrl(url) {
59
61
  return youtubeRegexp.test(url);
62
+ }
63
+ /**
64
+ * 参考: https://blueprintue.com/
65
+ * 生成されるURLをもとに正規表現を定義した
66
+ */
67
+
68
+
69
+ function isBlueprintUEUrl(url) {
70
+ return /^https:\/\/blueprintue\.com\/render\/.+\/$/.test(url);
71
+ }
72
+ /**
73
+ * 参考: https://www.figma.com/developers/embed
74
+ */
75
+
76
+
77
+ function isFigmaUrl(url) {
78
+ return /^https:\/\/([\w.-]+\.)?figma.com\/(file|proto)\/([0-9a-zA-Z]{22,128})(?:\/.*)?$/.test(url);
60
79
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zenn-markdown-html",
3
- "version": "0.1.121",
3
+ "version": "0.1.122-alpha.2",
4
4
  "license": "MIT",
5
5
  "description": "Convert markdown to zenn flavor html.",
6
6
  "main": "lib/index.js",
@@ -49,11 +49,11 @@
49
49
  "markdown-it-container": "^2.0.0",
50
50
  "markdown-it-footnote": "^3.0.3",
51
51
  "markdown-it-inline-comments": "^1.0.1",
52
- "markdown-it-link-attributes": "^3.0.0",
52
+ "markdown-it-link-attributes": "^4.0.1",
53
53
  "markdown-it-task-lists": "^2.1.1",
54
54
  "prismjs": "^1.27.0"
55
55
  },
56
- "gitHead": "33ab0a916ef1fb08271fe03fa694d11d7bd77e66",
56
+ "gitHead": "9e1bb555f5ecb60466b58287820f07ac4940da42",
57
57
  "publishConfig": {
58
58
  "access": "public"
59
59
  }