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

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
@@ -181,6 +181,7 @@ A paragraph.
181
181
  </figure>
182
182
  <p>A paragraph.</p>
183
183
 
184
+
184
185
  [Markdown]
185
186
  A paragraph.
186
187
 
@@ -196,8 +197,62 @@ A paragraph.
196
197
  <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>
197
198
  </figure>
198
199
  <p>A paragraph.</p>
200
+
201
+
202
+ [Markdown]
203
+ A paragraph.
204
+
205
+ Video. A youtube.
206
+
207
+ <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>
208
+
209
+ A paragraph.
210
+ [HTML]
211
+ <p>A paragraph.</p>
212
+ <figure class="f-video">
213
+ <figcaption><span class="f-video-label">Video<span class="f-video-label-joint">.</span></span> A youtube.</figcaption>
214
+ <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>
215
+ </figure>
216
+ <p>A paragraph.</p>
217
+
218
+
219
+ [Markdown]
220
+ A paragraph.
221
+
222
+ Quote. Twitter Post.
223
+
224
+ <blockquote class="twitter-tweet"><p lang="ja" dir="ltr">XXXXX <a href="https://t.co/XXXXX">https://t.co/XXXXX</a></p>&mdash; User (@twitter) <a href="https://twitter.com/UserID/status/XXXXX">August 4, 2022</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
225
+
226
+ A paragraph.
227
+ [HTML]
228
+ <p>A paragraph.</p>
229
+ <figure class="f-blockquote">
230
+ <figcaption><span class="f-blockquote-label">Quote<span class="f-blockquote-label-joint">.</span></span> Twitter Post.</figcaption>
231
+ <blockquote class="twitter-tweet"><p lang="ja" dir="ltr">XXXXX <a href="https://t.co/XXXXX">https://t.co/XXXXX</a></p>&mdash; User (@twitter) <a href="https://twitter.com/UserID/status/XXXXX">August 4, 2022</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
232
+ </figure>
233
+ <p>A paragraph.</p>
234
+
235
+
236
+ [Markdown]
237
+ A paragraph.
238
+
239
+ Figure. Twitter Post.
240
+
241
+ <blockquote class="twitter-tweet"><p lang="ja" dir="ltr">XXXXX <a href="https://t.co/XXXXX">https://t.co/XXXXX</a></p>&mdash; UserName (@account) <a href="https://twitter.com/account/status/XXXXX">August 4, 2022</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
242
+
243
+ A paragraph.
244
+ [HTML]
245
+ <p>A paragraph.</p>
246
+ <figure class="f-img">
247
+ <figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> Twitter Post.</figcaption>
248
+ <blockquote class="twitter-tweet"><p lang="ja" dir="ltr">XXXXX <a href="https://t.co/XXXXX">https://t.co/XXXXX</a></p>&mdash; User (@twitter) <a href="https://twitter.com/UserID/status/XXXXX">August 4, 2022</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
249
+ </figure>
250
+ <p>A paragraph.</p>
251
+
199
252
  ~~~
200
253
 
254
+ Note: External embedding supports Youtube and Twitter. Twitter embedding uses blockquote instead of iframe. Therefore, the caption identifier should use "Quote", but "Figure" is also acceptable.
255
+
201
256
 
202
257
  ## Option: Specify file name
203
258
 
package/index.js CHANGED
@@ -10,7 +10,11 @@ module.exports = function figure_with_caption_plugin(md, option) {
10
10
  scaleSuffix: false,
11
11
  dquoteFilename: false,
12
12
  strongFilename: false,
13
+ bLabel: false,
14
+ strongLabel: false,
15
+ jointSpaceUseHalfWidth: false,
13
16
  oneImageWithoutCaption: false,
17
+ removeUnnumberedLabel: false,
14
18
  };
15
19
  if (option !== undefined) {
16
20
  for (let o in option) {
@@ -22,6 +26,9 @@ module.exports = function figure_with_caption_plugin(md, option) {
22
26
  if(n < 3) {return caption;}
23
27
  const captionStartToken = state.tokens[n-3];
24
28
  const captionEndToken = state.tokens[n-1];
29
+ if (captionStartToken === undefined || captionEndToken === undefined) {
30
+ return caption;
31
+ }
25
32
  if (captionStartToken.type !== 'paragraph_open'
26
33
  && captionEndToken.type !== 'paragraph_close') {
27
34
  return caption;
@@ -68,6 +75,9 @@ module.exports = function figure_with_caption_plugin(md, option) {
68
75
  if (en + 2 > state.tokens.length) { return caption; }
69
76
  const captionStartToken = state.tokens[en+1];
70
77
  const captionEndToken = state.tokens[en+3];
78
+ if (captionStartToken === undefined || captionEndToken === undefined) {
79
+ return caption;
80
+ }
71
81
  if (captionStartToken.type !== 'paragraph_open'
72
82
  && captionEndToken.type !== 'paragraph_close') {
73
83
  return caption;
@@ -110,11 +120,16 @@ module.exports = function figure_with_caption_plugin(md, option) {
110
120
  return true;
111
121
  }
112
122
 
113
- function wrapWithFigure(state, range, tagName, replaceInsteadOfWrap) {
123
+ function wrapWithFigure(state, range, tagName, replaceInsteadOfWrap, sp) {
114
124
  let n = range.start;
115
125
  let en = range.end;
116
126
  const figureStartToken = new state.Token('figure_open', 'figure', 1);
117
127
  figureStartToken.attrSet('class', 'f-' + tagName);
128
+ if (sp) {
129
+ if (sp.isTwitterBlock && sp.hasImgCaption) {
130
+ figureStartToken.attrSet('class', 'f-img');
131
+ }
132
+ }
118
133
  if(/pre-(?:code|samp)/.test(tagName)) {
119
134
  figureStartToken.attrSet('role', 'doc-example');
120
135
  }
@@ -146,6 +161,7 @@ module.exports = function figure_with_caption_plugin(md, option) {
146
161
 
147
162
  function checkCaption(state, n, en, tagName, caption) {
148
163
  caption = checkPrevCaption(state, n, en, tagName, caption);
164
+ if (caption.hasPrev) return caption;
149
165
  caption = checkNextCaption(state, n, en, tagName, caption);
150
166
  return caption;
151
167
  }
@@ -216,24 +232,52 @@ module.exports = function figure_with_caption_plugin(md, option) {
216
232
  }
217
233
 
218
234
  if (token.type === 'html_block') {
219
- const tags = ['video', 'audio', 'iframe'];
235
+ const tags = ['video', 'audio', 'iframe', 'blockquote'];
220
236
  let ctj = 0;
237
+ let sp = {};
221
238
  while (ctj < tags.length) {
222
- const hasTag = token.content.match(new RegExp('^<'+ tags[ctj] + ' ?[^>]*?>[\\s\\S]*?<\\/' + tags[ctj] + '>\\n'));
239
+ const hasTag = token.content.match(new RegExp('^<'+ tags[ctj] + ' ?[^>]*?>[\\s\\S]*?<\\/' + tags[ctj] + '> *?(?:<script [^>]*?>(?:</script>)?)?\\n'));
223
240
  if (!hasTag) {
224
241
  ctj++;
225
242
  continue;
226
243
  }
227
244
  tagName = tags[ctj];
228
- if (tags[ctj] === 'iframe') {
245
+ if (tagName === 'iframe') {
229
246
  if(/^<[^>]*? title="YouTube video player"/i.test(token.content)) {
230
- tagName = 'video';
247
+ tagName = 'video'; // figure.f-video used.
248
+ }
249
+ // Non Youtube iframe currently use figure.f-iframe.
250
+ // Class in caption uses actual caption identifier.
251
+ }
252
+ if (tagName === 'blockquote') {
253
+ if(/^<[^>]*? class="twitter-tweet"/.test(token.content)) {
254
+ sp.isTwitterBlock = true;
255
+ if (n > 2) {
256
+ if (state.tokens[n-2].children.length > 1) {
257
+ if (state.tokens[n-2].children[1].attrs.length > 0) {
258
+ if (state.tokens[n-2].children[1].attrs[0][0] === 'class' &&
259
+ state.tokens[n-2].children[1].attrs[0][1] === 'f-img-label') {
260
+ sp.hasImgCaption = true;
261
+ }
262
+ }
263
+ }
264
+ }
265
+ if (n + 2 < state.tokens.length) {
266
+ if (state.tokens[n+2].children.length > 1) {
267
+ if (state.tokens[n+2].children[1].attrs.length > 0) {
268
+ if (state.tokens[n+2].children[1].attrs[0][0] === 'class' &&
269
+ state.tokens[n+2].children[1].attrs[0][1] === 'f-img-label') {
270
+ sp.hasImgCaption = true;
271
+ }
272
+ }
273
+ }
274
+ }
231
275
  }
232
276
  }
233
277
  checkToken = true;
234
278
  caption = checkCaption(state, n, en, tagName, caption);
235
279
  if (caption.hasPrev || caption.hasNext) {
236
- range = wrapWithFigure(state, range, tagName, false);
280
+ range = wrapWithFigure(state, range, tagName, false, sp);
237
281
  break;
238
282
  }
239
283
  ctj++
@@ -251,6 +295,9 @@ module.exports = function figure_with_caption_plugin(md, option) {
251
295
  tagName = 'img';
252
296
  nextToken.children[0].type = 'image';
253
297
  caption = checkCaption(state, n, en, tagName, caption);
298
+ if (opt.oneImageWithoutCaption && state.tokens[n-1]) {
299
+ if (state.tokens[n-1].type === 'list_item_open') {n++; continue;}
300
+ }
254
301
  if (opt.oneImageWithoutCaption || caption.hasPrev || caption.hasNext) {
255
302
  range = wrapWithFigure(state, range, tagName, true);
256
303
  }
@@ -280,6 +327,10 @@ module.exports = function figure_with_caption_plugin(md, option) {
280
327
  dquoteFilename: opt.dquoteFilename,
281
328
  strongFilename: opt.strongFilename,
282
329
  hasNumClass: opt.hasNumClass,
330
+ bLabel: opt.bLabel,
331
+ strongLabel: opt.strongLabel,
332
+ jointSpaceUseHalfWidth: opt.jointSpaceUseHalfWidth,
333
+ removeUnnumberedLabel: opt.removeUnnumberedLabel,
283
334
  });
284
335
  md.core.ruler.before('linkify', 'figure_with_caption', figureWithCaption);
285
336
  md.renderer.rules['fence_samp'] = function (tokens, idx, options, env, slf) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peaceroad/markdown-it-figure-with-p-caption",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
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": {
@@ -16,6 +16,6 @@
16
16
  "markdown-it": "^12.1.0"
17
17
  },
18
18
  "dependencies": {
19
- "p7d-markdown-it-p-captions": "^0.7.0"
19
+ "p7d-markdown-it-p-captions": "^0.9.0"
20
20
  }
21
21
  }
package/test/examples.txt CHANGED
@@ -372,9 +372,47 @@ A paragraph.
372
372
  </figure>
373
373
  <p>A paragraph.</p>
374
374
 
375
+
376
+ [Markdown]
377
+ A paragraph.
378
+
379
+ Figure. A cat.
380
+
381
+ ![Figure](cat.jpg)
382
+
383
+ A paragraph.
384
+
385
+ Video. A mp4.
386
+
387
+ <video controls width="400" height="300">
388
+ <source src="example.mp4" type="video/mp4">
389
+ </video>
390
+
391
+ A paragraph.
392
+
393
+ [HTML]
394
+ <p>A paragraph.</p>
395
+ <figure class="f-img">
396
+ <figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> A cat.</figcaption>
397
+ <img src="cat.jpg" alt="Figure">
398
+ </figure>
399
+ <p>A paragraph.</p>
400
+ <figure class="f-video">
401
+ <figcaption><span class="f-video-label">Video<span class="f-video-label-joint">.</span></span> A mp4.</figcaption>
402
+ <video controls width="400" height="300">
403
+ <source src="example.mp4" type="video/mp4">
404
+ </video>
405
+ </figure>
406
+ <p>A paragraph.</p>
407
+
408
+
375
409
  [Markdown]
376
410
  A paragraph.
377
411
 
412
+ Figure. A cat.
413
+
414
+ ![Figure](cat.jpg)
415
+
378
416
  Video. A mp4.
379
417
 
380
418
  <video controls width="400" height="300">
@@ -382,8 +420,13 @@ Video. A mp4.
382
420
  </video>
383
421
 
384
422
  A paragraph.
423
+
385
424
  [HTML]
386
425
  <p>A paragraph.</p>
426
+ <figure class="f-img">
427
+ <figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> A cat.</figcaption>
428
+ <img src="cat.jpg" alt="Figure">
429
+ </figure>
387
430
  <figure class="f-video">
388
431
  <figcaption><span class="f-video-label">Video<span class="f-video-label-joint">.</span></span> A mp4.</figcaption>
389
432
  <video controls width="400" height="300">
@@ -392,6 +435,7 @@ A paragraph.
392
435
  </figure>
393
436
  <p>A paragraph.</p>
394
437
 
438
+
395
439
  [Markdown]
396
440
  A paragraph.
397
441
 
@@ -409,6 +453,83 @@ A paragraph.
409
453
  <p>A paragraph.</p>
410
454
 
411
455
 
456
+ [Markdown]
457
+ A paragraph.
458
+
459
+ Quote. Twitter Post.
460
+
461
+ <blockquote class="twitter-tweet"><p lang="ja" dir="ltr">XXXXX <a href="https://t.co/XXXXX">https://t.co/XXXXX</a></p>&mdash; User (@twitter) <a href="https://twitter.com/UserID/status/XXXXX">August 4, 2022</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
462
+
463
+ A paragraph.
464
+ [HTML]
465
+ <p>A paragraph.</p>
466
+ <figure class="f-blockquote">
467
+ <figcaption><span class="f-blockquote-label">Quote<span class="f-blockquote-label-joint">.</span></span> Twitter Post.</figcaption>
468
+ <blockquote class="twitter-tweet"><p lang="ja" dir="ltr">XXXXX <a href="https://t.co/XXXXX">https://t.co/XXXXX</a></p>&mdash; User (@twitter) <a href="https://twitter.com/UserID/status/XXXXX">August 4, 2022</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
469
+ </figure>
470
+ <p>A paragraph.</p>
471
+
472
+ [Markdown]
473
+ A paragraph.
474
+
475
+ Figure. Twitter Post.
476
+
477
+ <blockquote class="twitter-tweet"><p lang="ja" dir="ltr">XXXXX <a href="https://t.co/XXXXX">https://t.co/XXXXX</a></p>&mdash; User (@twitter) <a href="https://twitter.com/UserID/status/XXXXX">August 4, 2022</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
478
+
479
+ A paragraph.
480
+ [HTML]
481
+ <p>A paragraph.</p>
482
+ <figure class="f-img">
483
+ <figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> Twitter Post.</figcaption>
484
+ <blockquote class="twitter-tweet"><p lang="ja" dir="ltr">XXXXX <a href="https://t.co/XXXXX">https://t.co/XXXXX</a></p>&mdash; User (@twitter) <a href="https://twitter.com/UserID/status/XXXXX">August 4, 2022</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
485
+ </figure>
486
+ <p>A paragraph.</p>
487
+
488
+ [Markdown]
489
+ A paragraph.
490
+
491
+ <blockquote class="twitter-tweet"><p lang="ja" dir="ltr">XXXXX <a href="https://t.co/XXXXX">https://t.co/XXXXX</a></p>&mdash; User (@twitter) <a href="https://twitter.com/UserID/status/XXXXX">August 4, 2022</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
492
+
493
+ Figure. Twitter Post.
494
+
495
+ A paragraph.
496
+ [HTML]
497
+ <p>A paragraph.</p>
498
+ <figure class="f-img">
499
+ <blockquote class="twitter-tweet"><p lang="ja" dir="ltr">XXXXX <a href="https://t.co/XXXXX">https://t.co/XXXXX</a></p>&mdash; User (@twitter) <a href="https://twitter.com/UserID/status/XXXXX">August 4, 2022</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
500
+ <figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> Twitter Post.</figcaption>
501
+ </figure>
502
+ <p>A paragraph.</p>
503
+
504
+ [Markdown]
505
+ A paragraph.
506
+
507
+ <blockquote class="twitter-tweet"><p lang="ja" dir="ltr">XXXXX <a href="https://t.co/XXXXX">https://t.co/XXXXX</a></p>&mdash; User (@twitter) <a href="https://twitter.com/UserID/status/XXXXX">August 4, 2022</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
508
+
509
+ Quote. Twitter Post.
510
+
511
+ A paragraph.
512
+ [HTML]
513
+ <p>A paragraph.</p>
514
+ <figure class="f-blockquote">
515
+ <blockquote class="twitter-tweet"><p lang="ja" dir="ltr">XXXXX <a href="https://t.co/XXXXX">https://t.co/XXXXX</a></p>&mdash; User (@twitter) <a href="https://twitter.com/UserID/status/XXXXX">August 4, 2022</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
516
+ <figcaption><span class="f-blockquote-label">Quote<span class="f-blockquote-label-joint">.</span></span> Twitter Post.</figcaption>
517
+ </figure>
518
+ <p>A paragraph.</p>
519
+
520
+
521
+ [Markdown]
522
+ A paragraph.
523
+
524
+ <blockquote class="twitter-tweet"><p lang="ja" dir="ltr">XXXXX <a href="https://t.co/XXXXX">https://t.co/XXXXX</a></p>&mdash; User (@twitter) <a href="https://twitter.com/UserID/status/XXXXX">August 4, 2022</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
525
+
526
+ A paragraph.
527
+ [HTML]
528
+ <p>A paragraph.</p>
529
+ <blockquote class="twitter-tweet"><p lang="ja" dir="ltr">XXXXX <a href="https://t.co/XXXXX">https://t.co/XXXXX</a></p>&mdash; User (@twitter) <a href="https://twitter.com/UserID/status/XXXXX">August 4, 2022</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
530
+ <p>A paragraph.</p>
531
+
532
+
412
533
  [Markdown]
413
534
  A paragraph.
414
535
 
@@ -509,6 +630,21 @@ A paragraph.
509
630
  <p>A paragraph.</p>
510
631
 
511
632
 
633
+ [Markdown]
634
+ A paragraph.
635
+
636
+ - ![Figure](cat.jpg)
637
+
638
+ A paragraph.
639
+
640
+ [HTML]
641
+ <p>A paragraph.</p>
642
+ <ul>
643
+ <li><img src="cat.jpg" alt="Figure"></li>
644
+ </ul>
645
+ <p>A paragraph.</p>
646
+
647
+
512
648
  [Markdown]
513
649
  A paragraph.
514
650
 
@@ -529,3 +665,4 @@ A paragraph.
529
665
  </figure>
530
666
  <p>A paragraph.</p>
531
667
 
668
+
package/test/test.js CHANGED
@@ -1,12 +1,18 @@
1
1
  const assert = require('assert');
2
2
  const fs = require('fs');
3
- const md = require('markdown-it')({
4
- html: true,
5
- });
3
+ const md = require('markdown-it')({ html: true });
4
+ const mdOneImage = require('markdown-it')({ html: true });
6
5
  const mdFigureWithPCaption = require('../index.js');
7
6
 
8
7
 
9
8
  md.use(mdFigureWithPCaption, {
9
+ dquoteFilename: true,
10
+ strongFilename: true,
11
+ oneImageWithoutCaption: false,
12
+ hasNumClass: true,
13
+ });
14
+
15
+ mdOneImage.use(mdFigureWithPCaption, {
10
16
  dquoteFilename: true,
11
17
  strongFilename: true,
12
18
  oneImageWithoutCaption: true,
@@ -40,12 +46,18 @@ while(n < ms0.length) {
40
46
 
41
47
  n = 1;
42
48
  while(n < ms.length) {
43
- //if (n !== 21) { n++; continue };
49
+ //if (n !== 22) { n++; continue };
44
50
  console.log('Test: ' + n + ' >>>');
45
51
  //console.log(ms[n].markdown);
46
52
 
47
53
  const m = ms[n].markdown;
48
- const h = md.render(m);
54
+ let h = ''
55
+ if (n > 20) {
56
+ h = mdOneImage.render(m);
57
+ } else {
58
+ h = md.render(m);
59
+ }
60
+
49
61
  try {
50
62
  assert.strictEqual(h, ms[n].html);
51
63
  } catch(e) {