@peaceroad/markdown-it-figure-with-p-caption 0.3.3 → 0.4.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
@@ -10,6 +10,8 @@ For a paragraph with only one image, a table or code block or a blockquote, and
10
10
 
11
11
  The figcaption behavior of this plugin depends on [p7d-markdown-it-p-captions](https://www.npmjs.com/package/p7d-markdown-it-p-captions).
12
12
 
13
+ Note. If code block language setting is "samp", change it to use samp element instead of code element.
14
+
13
15
  Use it as follows.
14
16
 
15
17
  ```js
@@ -27,10 +29,10 @@ console.log(md.render('Figure. A Cat.\n\n![Figure](cat.jpg)');
27
29
 
28
30
  Also, It is recommended to set the width and height attributes of the images at the same time. See: [@peaceroad/markdown-it-renderer-image](https://www.npmjs.com/package/@peaceroad/markdown-it-renderer-image).
29
31
 
30
- It could be applied to table, codeblock(pre > code, pre > samp), video, audio as well.
32
+ It could be applied to table, codeblock(pre > code, pre > samp), video as well.
31
33
 
32
- Example:
33
34
 
35
+ ## Example
34
36
 
35
37
  ~~~
36
38
  [Markdown]
@@ -157,8 +159,100 @@ A paragraph.
157
159
  </samp></pre>
158
160
  </figure>
159
161
  <p>A paragraph.</p>
162
+
163
+
164
+ [Markdown]
165
+ A paragraph.
166
+
167
+ Video. A mp4.
168
+
169
+ <video controls width="400" height="300">
170
+ <source src="example.mp4" type="video/mp4">
171
+ </video>
172
+
173
+ A paragraph.
174
+ [HTML]
175
+ <p>A paragraph.</p>
176
+ <figure class="f-video">
177
+ <figcaption><span class="f-video-label">Video<span class="f-video-label-joint">.</span></span> A mp4.</figcaption>
178
+ <video controls width="400" height="300">
179
+ <source src="example.mp4" type="video/mp4">
180
+ </video>
181
+ </figure>
182
+ <p>A paragraph.</p>
183
+
184
+
185
+ [Markdown]
186
+ A paragraph.
187
+
188
+ Video. A youtube.
189
+
190
+ <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>
191
+
192
+ A paragraph.
193
+ [HTML]
194
+ <p>A paragraph.</p>
195
+ <figure class="f-video">
196
+ <figcaption><span class="f-video-label">Video<span class="f-video-label-joint">.</span></span> A youtube.</figcaption>
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>
198
+ </figure>
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
+
160
252
  ~~~
161
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
+
162
256
 
163
257
  ## Option: Specify file name
164
258
 
@@ -222,3 +316,27 @@ A paragraph.
222
316
  </figure>
223
317
  <p>A paragraph.</p>
224
318
  ~~~
319
+
320
+ ## Option: Convert one image paragraph without caption
321
+
322
+ Convert one image paragraph without a caption paragraph to figure element.
323
+
324
+ ```js
325
+ md.use(mdFigureWithPCaption, {oneImageWithoutCaption: true});
326
+ ```
327
+
328
+ ~~~
329
+ [Markdown]
330
+ A paragraph.
331
+
332
+ ![Figure](cat.jpg)
333
+
334
+ A paragraph.
335
+
336
+ [HTML]
337
+ <p>A paragraph.</p>
338
+ <figure class="f-img">
339
+ <img src="cat.jpg" alt="Figure">
340
+ </figure>
341
+ <p>A paragraph.</p>
342
+ ~~~
package/index.js CHANGED
@@ -1,16 +1,19 @@
1
1
  'use strict';
2
2
 
3
- const { has } = require('markdown-it/lib/common/utils');
4
-
5
3
  module.exports = function figure_with_caption_plugin(md, option) {
6
4
 
7
5
  const mdPCaption = require('p7d-markdown-it-p-captions');
8
6
 
9
7
  let opt = {
10
8
  classPrefix: 'f',
9
+ hasNumClass: false,
11
10
  scaleSuffix: false,
12
11
  dquoteFilename: false,
13
12
  strongFilename: false,
13
+ bLabel: false,
14
+ strongLabel: false,
15
+ jointSpaceUseHalfWidth: false,
16
+ oneImageWithoutCaption: false,
14
17
  };
15
18
  if (option !== undefined) {
16
19
  for (let o in option) {
@@ -110,11 +113,16 @@ module.exports = function figure_with_caption_plugin(md, option) {
110
113
  return true;
111
114
  }
112
115
 
113
- function wrapWithFigure(state, range, tagName, replaceInsteadOfWrap) {
116
+ function wrapWithFigure(state, range, tagName, replaceInsteadOfWrap, sp) {
114
117
  let n = range.start;
115
118
  let en = range.end;
116
119
  const figureStartToken = new state.Token('figure_open', 'figure', 1);
117
120
  figureStartToken.attrSet('class', 'f-' + tagName);
121
+ if (sp) {
122
+ if (sp.isTwitterBlock && sp.hasImgCaption) {
123
+ figureStartToken.attrSet('class', 'f-img');
124
+ }
125
+ }
118
126
  if(/pre-(?:code|samp)/.test(tagName)) {
119
127
  figureStartToken.attrSet('role', 'doc-example');
120
128
  }
@@ -216,24 +224,52 @@ module.exports = function figure_with_caption_plugin(md, option) {
216
224
  }
217
225
 
218
226
  if (token.type === 'html_block') {
219
- const tags = ['video', 'audio', 'iframe'];
227
+ const tags = ['video', 'audio', 'iframe', 'blockquote'];
220
228
  let ctj = 0;
229
+ let sp = {};
221
230
  while (ctj < tags.length) {
222
- const hasTag = token.content.match(new RegExp('^<'+ tags[ctj] + ' ?[^>]*?>[\\s\\S]*?<\\/' + tags[ctj] + '>\\n'));
231
+ const hasTag = token.content.match(new RegExp('^<'+ tags[ctj] + ' ?[^>]*?>[\\s\\S]*?<\\/' + tags[ctj] + '> *?(?:<script [^>]*?>(?:</script>)?)?\\n'));
223
232
  if (!hasTag) {
224
233
  ctj++;
225
234
  continue;
226
235
  }
227
236
  tagName = tags[ctj];
228
- if (tags[ctj] === 'iframe') {
237
+ if (tagName === 'iframe') {
229
238
  if(/^<[^>]*? title="YouTube video player"/i.test(token.content)) {
230
- tagName = 'video';
239
+ tagName = 'video'; // figure.f-video used.
240
+ }
241
+ // Non Youtube iframe currently use figure.f-iframe.
242
+ // Class in caption uses actual caption identifier.
243
+ }
244
+ if (tagName === 'blockquote') {
245
+ if(/^<[^>]*? class="twitter-tweet"/.test(token.content)) {
246
+ sp.isTwitterBlock = true;
247
+ if (n > 2) {
248
+ if (state.tokens[n-2].children.length > 1) {
249
+ if (state.tokens[n-2].children[1].attrs.length > 0) {
250
+ if (state.tokens[n-2].children[1].attrs[0][0] === 'class' &&
251
+ state.tokens[n-2].children[1].attrs[0][1] === 'f-img-label') {
252
+ sp.hasImgCaption = true;
253
+ }
254
+ }
255
+ }
256
+ }
257
+ if (n + 2 < state.tokens.length) {
258
+ if (state.tokens[n+2].children.length > 1) {
259
+ if (state.tokens[n+2].children[1].attrs.length > 0) {
260
+ if (state.tokens[n+2].children[1].attrs[0][0] === 'class' &&
261
+ state.tokens[n+2].children[1].attrs[0][1] === 'f-img-label') {
262
+ sp.hasImgCaption = true;
263
+ }
264
+ }
265
+ }
266
+ }
231
267
  }
232
268
  }
233
269
  checkToken = true;
234
270
  caption = checkCaption(state, n, en, tagName, caption);
235
271
  if (caption.hasPrev || caption.hasNext) {
236
- range = wrapWithFigure(state, range, tagName, false);
272
+ range = wrapWithFigure(state, range, tagName, false, sp);
237
273
  break;
238
274
  }
239
275
  ctj++
@@ -251,7 +287,10 @@ module.exports = function figure_with_caption_plugin(md, option) {
251
287
  tagName = 'img';
252
288
  nextToken.children[0].type = 'image';
253
289
  caption = checkCaption(state, n, en, tagName, caption);
254
- if (caption.hasPrev || caption.hasNext) {
290
+ if (opt.oneImageWithoutCaption && state.tokens[n-1]) {
291
+ if (state.tokens[n-1].type === 'list_item_open') {n++; continue;}
292
+ }
293
+ if (opt.oneImageWithoutCaption || caption.hasPrev || caption.hasNext) {
255
294
  range = wrapWithFigure(state, range, tagName, true);
256
295
  }
257
296
  }
@@ -279,6 +318,10 @@ module.exports = function figure_with_caption_plugin(md, option) {
279
318
  classPrefix: opt.classPrefix,
280
319
  dquoteFilename: opt.dquoteFilename,
281
320
  strongFilename: opt.strongFilename,
321
+ hasNumClass: opt.hasNumClass,
322
+ bLabel: opt.bLabel,
323
+ strongLabel: opt.strongLabel,
324
+ jointSpaceUseHalfWidth: opt.jointSpaceUseHalfWidth,
282
325
  });
283
326
  md.core.ruler.before('linkify', 'figure_with_caption', figureWithCaption);
284
327
  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.3.3",
3
+ "version": "0.4.2",
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.6.3"
19
+ "p7d-markdown-it-p-captions": "^0.8.0"
20
20
  }
21
21
  }
package/test/examples.txt CHANGED
@@ -266,7 +266,6 @@ A paragraph.
266
266
  <p><img src="cat.jpg" alt="Figure"></p>
267
267
 
268
268
 
269
-
270
269
  [Markdown]
271
270
  Figure. A cat.
272
271
 
@@ -286,7 +285,7 @@ Figure 12. A cat.
286
285
 
287
286
  [HTML]
288
287
  <figure class="f-img">
289
- <figcaption><span class="f-img-label">Figure 12<span class="f-img-label-joint">.</span></span> A cat.</figcaption>
288
+ <figcaption><span class="f-img-label label-has-num">Figure 12<span class="f-img-label-joint">.</span></span> A cat.</figcaption>
290
289
  <img src="cat.jpg" alt="Figure">
291
290
  </figure>
292
291
 
@@ -410,6 +409,83 @@ A paragraph.
410
409
  <p>A paragraph.</p>
411
410
 
412
411
 
412
+ [Markdown]
413
+ A paragraph.
414
+
415
+ Quote. Twitter Post.
416
+
417
+ <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>
418
+
419
+ A paragraph.
420
+ [HTML]
421
+ <p>A paragraph.</p>
422
+ <figure class="f-blockquote">
423
+ <figcaption><span class="f-blockquote-label">Quote<span class="f-blockquote-label-joint">.</span></span> Twitter Post.</figcaption>
424
+ <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>
425
+ </figure>
426
+ <p>A paragraph.</p>
427
+
428
+ [Markdown]
429
+ A paragraph.
430
+
431
+ Figure. Twitter Post.
432
+
433
+ <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>
434
+
435
+ A paragraph.
436
+ [HTML]
437
+ <p>A paragraph.</p>
438
+ <figure class="f-img">
439
+ <figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> Twitter Post.</figcaption>
440
+ <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>
441
+ </figure>
442
+ <p>A paragraph.</p>
443
+
444
+ [Markdown]
445
+ A paragraph.
446
+
447
+ <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>
448
+
449
+ Figure. Twitter Post.
450
+
451
+ A paragraph.
452
+ [HTML]
453
+ <p>A paragraph.</p>
454
+ <figure class="f-img">
455
+ <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>
456
+ <figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> Twitter Post.</figcaption>
457
+ </figure>
458
+ <p>A paragraph.</p>
459
+
460
+ [Markdown]
461
+ A paragraph.
462
+
463
+ <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>
464
+
465
+ Quote. Twitter Post.
466
+
467
+ A paragraph.
468
+ [HTML]
469
+ <p>A paragraph.</p>
470
+ <figure class="f-blockquote">
471
+ <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>
472
+ <figcaption><span class="f-blockquote-label">Quote<span class="f-blockquote-label-joint">.</span></span> Twitter Post.</figcaption>
473
+ </figure>
474
+ <p>A paragraph.</p>
475
+
476
+
477
+ [Markdown]
478
+ A paragraph.
479
+
480
+ <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>
481
+
482
+ A paragraph.
483
+ [HTML]
484
+ <p>A paragraph.</p>
485
+ <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>
486
+ <p>A paragraph.</p>
487
+
488
+
413
489
  [Markdown]
414
490
  A paragraph.
415
491
 
@@ -489,3 +565,59 @@ A paragraph.
489
565
  </code></pre>
490
566
  </figure>
491
567
  <p>A paragraph.</p>
568
+
569
+ [Markdown]
570
+ ![Figure](cat.jpg)
571
+
572
+ A paragraph.
573
+
574
+ ![Figure](cat.jpg)
575
+
576
+ A paragraph.
577
+
578
+ [HTML]
579
+ <figure class="f-img">
580
+ <img src="cat.jpg" alt="Figure">
581
+ </figure>
582
+ <p>A paragraph.</p>
583
+ <figure class="f-img">
584
+ <img src="cat.jpg" alt="Figure">
585
+ </figure>
586
+ <p>A paragraph.</p>
587
+
588
+
589
+ [Markdown]
590
+ A paragraph.
591
+
592
+ - ![Figure](cat.jpg)
593
+
594
+ A paragraph.
595
+
596
+ [HTML]
597
+ <p>A paragraph.</p>
598
+ <ul>
599
+ <li><img src="cat.jpg" alt="Figure"></li>
600
+ </ul>
601
+ <p>A paragraph.</p>
602
+
603
+
604
+ [Markdown]
605
+ A paragraph.
606
+
607
+ リスト1 キャプション
608
+
609
+ ```js
610
+ console.log('Nyaan!');
611
+ ```
612
+
613
+ A paragraph.
614
+
615
+ [HTML]
616
+ <p>A paragraph.</p>
617
+ <figure class="f-pre-code" role="doc-example">
618
+ <figcaption><span class="f-pre-code-label label-has-num">リスト1</span> キャプション</figcaption>
619
+ <pre><code class="language-js">console.log('Nyaan!');
620
+ </code></pre>
621
+ </figure>
622
+ <p>A paragraph.</p>
623
+
package/test/test.js CHANGED
@@ -5,11 +5,15 @@ const md = require('markdown-it')({
5
5
  });
6
6
  const mdFigureWithPCaption = require('../index.js');
7
7
 
8
+
8
9
  md.use(mdFigureWithPCaption, {
9
10
  dquoteFilename: true,
10
11
  strongFilename: true,
12
+ oneImageWithoutCaption: true,
13
+ hasNumClass: true,
11
14
  });
12
15
 
16
+
13
17
  const example = __dirname + '/examples.txt';
14
18
  const mdPath = __dirname + '/examples.md';
15
19
  const exampleCont = fs.readFileSync(example, 'utf-8').trim();
@@ -36,7 +40,7 @@ while(n < ms0.length) {
36
40
 
37
41
  n = 1;
38
42
  while(n < ms.length) {
39
- //if (n !== 21) { n++; continue };
43
+ //if (n !== 24) { n++; continue };
40
44
  console.log('Test: ' + n + ' >>>');
41
45
  //console.log(ms[n].markdown);
42
46