@peaceroad/markdown-it-figure-with-p-caption 0.4.3 → 0.4.5

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
@@ -317,7 +317,9 @@ A paragraph.
317
317
  <p>A paragraph.</p>
318
318
  ~~~
319
319
 
320
- ## Option: Convert one image paragraph without caption
320
+ ## Option
321
+
322
+ ### Convert one image paragraph without caption
321
323
 
322
324
  Convert one image paragraph without a caption paragraph to figure element.
323
325
 
@@ -340,3 +342,26 @@ A paragraph.
340
342
  </figure>
341
343
  <p>A paragraph.</p>
342
344
  ~~~
345
+
346
+ #### Convert one iframe without caption
347
+
348
+ Convert one iframe without a caption paragraph to figure element.
349
+
350
+ ```js
351
+ md.use(mdFigureWithPCaption, {iframeWithoutCaption: true});
352
+ ```
353
+
354
+ ~~~
355
+ [Markdown]
356
+ A paragraph.
357
+
358
+ <iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/XXXXXXXXXXX" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
359
+
360
+ A paragraph.
361
+ [HTML]
362
+ <p>A paragraph.</p>
363
+ <figure class="f-video">
364
+ <iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/XXXXXXXXXXX" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
365
+ </figure>
366
+ <p>A paragraph.</p>
367
+ ~~~
package/index.js CHANGED
@@ -14,6 +14,7 @@ module.exports = function figure_with_caption_plugin(md, option) {
14
14
  strongLabel: false,
15
15
  jointSpaceUseHalfWidth: false,
16
16
  oneImageWithoutCaption: false,
17
+ iframeWithoutCaption: false,
17
18
  removeUnnumberedLabel: false,
18
19
  };
19
20
  if (option !== undefined) {
@@ -236,11 +237,14 @@ module.exports = function figure_with_caption_plugin(md, option) {
236
237
  let ctj = 0;
237
238
  let sp = {};
238
239
  while (ctj < tags.length) {
239
- const hasTag = token.content.match(new RegExp('^<'+ tags[ctj] + ' ?[^>]*?>[\\s\\S]*?<\\/' + tags[ctj] + '> *?(?:<script [^>]*?>(?:</script>)?)?\\n'));
240
+ const hasTag = token.content.match(new RegExp('^<'+ tags[ctj] + ' ?[^>]*?>[\\s\\S]*?<\\/' + tags[ctj] + '> *?(?:<script [^>]*?>(?:</script>)?)?(\\n|$)'));
240
241
  if (!hasTag) {
241
242
  ctj++;
242
243
  continue;
243
244
  }
245
+ if (hasTag[hasTag.length - 1] !== '\n') {
246
+ token.content += '\n'
247
+ }
244
248
  tagName = tags[ctj];
245
249
  if (tagName === 'iframe') {
246
250
  if(/^<[^>]*? title="YouTube video player"/i.test(token.content)) {
@@ -276,19 +280,24 @@ module.exports = function figure_with_caption_plugin(md, option) {
276
280
  }
277
281
  checkToken = true;
278
282
  caption = checkCaption(state, n, en, tagName, caption);
279
- if (caption.hasPrev || caption.hasNext) {
283
+ if (opt.iframeWithoutCaption || caption.hasPrev || caption.hasNext) {
280
284
  range = wrapWithFigure(state, range, tagName, false, sp);
285
+ if (opt.iframeWithoutCaption && (!caption.hasPrev || !caption.hasNext)) {
286
+ n = en + 2;
287
+ }
281
288
  break;
282
289
  }
283
290
  ctj++
284
291
  }
285
292
  }
286
293
 
287
- if (token.type === 'paragraph_open'
288
- && nextToken.type === 'inline'
289
- && nextToken.children[0].type === 'image'
290
- && nextToken.children.length === 1
291
- && state.tokens[n+2].type === 'paragraph_close') {
294
+
295
+ if (token.type === 'paragraph_open' && nextToken.type === 'inline' && nextToken.children[0].type === 'image' && state.tokens[n+2].type === 'paragraph_close' && nextToken.children.length < 3) {
296
+ if (nextToken.children.length === 2) {
297
+ if (!nextToken.children[nextToken.children.length - 1].type === 'text' || !/^ *?\{.*?\}$/.test(nextToken.children[nextToken.children.length - 1].content)) {
298
+ n++; continue;
299
+ }
300
+ }
292
301
  checkToken = true;
293
302
  en = n + 2;
294
303
  range.end = en;
@@ -296,9 +305,9 @@ module.exports = function figure_with_caption_plugin(md, option) {
296
305
  nextToken.children[0].type = 'image';
297
306
  caption = checkCaption(state, n, en, tagName, caption);
298
307
  if (opt.oneImageWithoutCaption && state.tokens[n-1]) {
299
- if (state.tokens[n-1].type === 'list_item_open') {n++; continue;}
308
+ if (state.tokens[n-1].type === 'list_item_open') {checkToken = false;}
300
309
  }
301
- if (opt.oneImageWithoutCaption || caption.hasPrev || caption.hasNext) {
310
+ if (checkToken && (opt.oneImageWithoutCaption || caption.hasPrev || caption.hasNext)) {
302
311
  range = wrapWithFigure(state, range, tagName, true);
303
312
  }
304
313
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peaceroad/markdown-it-figure-with-p-caption",
3
- "version": "0.4.3",
3
+ "version": "0.4.5",
4
4
  "description": "A markdown-it plugin. For a paragraph with only one image, a table or code block or blockquote, and by writing a caption paragraph immediately before or after, they are converted into the figure element with the figcaption element.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -13,7 +13,8 @@
13
13
  "url": "https://github.com/peaceroad/p7d-markdown-it-figure-with-p-caption/issues"
14
14
  },
15
15
  "devDependencies": {
16
- "markdown-it": "^12.1.0"
16
+ "markdown-it": "^12.1.0",
17
+ "markdown-it-attrs": "^4.1.4"
17
18
  },
18
19
  "dependencies": {
19
20
  "p7d-markdown-it-p-captions": "^0.9.0"
package/test/examples.txt CHANGED
@@ -666,3 +666,49 @@ A paragraph.
666
666
  <p>A paragraph.</p>
667
667
 
668
668
 
669
+ [Markdown]
670
+ Figure. A Caption.
671
+
672
+ ![Figure](cat.jpg) {.style}
673
+ [HTML]
674
+ <figure class="f-img style">
675
+ <figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> A Caption.</figcaption>
676
+ <img src="cat.jpg" alt="Figure">
677
+ </figure>
678
+
679
+ [Markdown]
680
+ Figure. A Caption.
681
+
682
+ ![Figure](cat.jpg) Text {.style}
683
+ [HTML]
684
+ <p class="f-img"><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> A Caption.</p>
685
+ <p class="style"><img src="cat.jpg" alt="Figure"> Text</p>
686
+
687
+
688
+ [Markdown]
689
+ A paragraph.
690
+
691
+ <iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/XXXXXXXXXXX" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
692
+
693
+ A paragraph.
694
+ [HTML]
695
+ <p>A paragraph.</p>
696
+ <figure class="f-video">
697
+ <iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/XXXXXXXXXXX" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
698
+ </figure>
699
+ <p>A paragraph.</p>
700
+
701
+
702
+ [Markdown]
703
+ <iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/XXXXXXXXXXX" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
704
+ [HTML]
705
+ <figure class="f-video">
706
+ <iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/XXXXXXXXXXX" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
707
+ </figure>
708
+
709
+ [Markdown]
710
+ <iframe src="https://example.com/embed" class="mastodon-embed" style="max-width: 100%; border: 0" width="400" allowfullscreen="allowfullscreen"></iframe><script src="https://exapmle.com/embed.js" async="async"></script>
711
+ [HTML]
712
+ <figure class="f-iframe">
713
+ <iframe src="https://example.com/embed" class="mastodon-embed" style="max-width: 100%; border: 0" width="400" allowfullscreen="allowfullscreen"></iframe><script src="https://exapmle.com/embed.js" async="async"></script>
714
+ </figure>
package/test/test.js CHANGED
@@ -2,8 +2,11 @@ const assert = require('assert');
2
2
  const fs = require('fs');
3
3
  const md = require('markdown-it')({ html: true });
4
4
  const mdOneImage = require('markdown-it')({ html: true });
5
+ const mdWithoutCaption = require('markdown-it')({ html: true });
6
+
5
7
  const mdFigureWithPCaption = require('../index.js');
6
8
 
9
+ const attrs = require('../node_modules/markdown-it-attrs');
7
10
 
8
11
  md.use(mdFigureWithPCaption, {
9
12
  dquoteFilename: true,
@@ -17,8 +20,15 @@ mdOneImage.use(mdFigureWithPCaption, {
17
20
  strongFilename: true,
18
21
  oneImageWithoutCaption: true,
19
22
  hasNumClass: true,
20
- });
23
+ }).use(attrs);
21
24
 
25
+ mdWithoutCaption.use(mdFigureWithPCaption, {
26
+ dquoteFilename: true,
27
+ strongFilename: true,
28
+ oneImageWithoutCaption: true,
29
+ iframeWithoutCaption: true,
30
+ hasNumClass: true,
31
+ }).use(attrs);
22
32
 
23
33
  const example = __dirname + '/examples.txt';
24
34
  const mdPath = __dirname + '/examples.md';
@@ -52,7 +62,9 @@ while(n < ms.length) {
52
62
 
53
63
  const m = ms[n].markdown;
54
64
  let h = ''
55
- if (n > 20) {
65
+ if (n > 37) {
66
+ h = mdWithoutCaption.render(m);
67
+ } else if (n > 20) {
56
68
  h = mdOneImage.render(m);
57
69
  } else {
58
70
  h = md.render(m);