@peaceroad/markdown-it-figure-with-p-caption 0.4.2 → 0.4.4

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/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
+ removeUnnumberedLabel: false,
17
18
  };
18
19
  if (option !== undefined) {
19
20
  for (let o in option) {
@@ -25,6 +26,9 @@ module.exports = function figure_with_caption_plugin(md, option) {
25
26
  if(n < 3) {return caption;}
26
27
  const captionStartToken = state.tokens[n-3];
27
28
  const captionEndToken = state.tokens[n-1];
29
+ if (captionStartToken === undefined || captionEndToken === undefined) {
30
+ return caption;
31
+ }
28
32
  if (captionStartToken.type !== 'paragraph_open'
29
33
  && captionEndToken.type !== 'paragraph_close') {
30
34
  return caption;
@@ -71,6 +75,9 @@ module.exports = function figure_with_caption_plugin(md, option) {
71
75
  if (en + 2 > state.tokens.length) { return caption; }
72
76
  const captionStartToken = state.tokens[en+1];
73
77
  const captionEndToken = state.tokens[en+3];
78
+ if (captionStartToken === undefined || captionEndToken === undefined) {
79
+ return caption;
80
+ }
74
81
  if (captionStartToken.type !== 'paragraph_open'
75
82
  && captionEndToken.type !== 'paragraph_close') {
76
83
  return caption;
@@ -154,6 +161,7 @@ module.exports = function figure_with_caption_plugin(md, option) {
154
161
 
155
162
  function checkCaption(state, n, en, tagName, caption) {
156
163
  caption = checkPrevCaption(state, n, en, tagName, caption);
164
+ if (caption.hasPrev) return caption;
157
165
  caption = checkNextCaption(state, n, en, tagName, caption);
158
166
  return caption;
159
167
  }
@@ -276,11 +284,13 @@ module.exports = function figure_with_caption_plugin(md, option) {
276
284
  }
277
285
  }
278
286
 
279
- if (token.type === 'paragraph_open'
280
- && nextToken.type === 'inline'
281
- && nextToken.children[0].type === 'image'
282
- && nextToken.children.length === 1
283
- && state.tokens[n+2].type === 'paragraph_close') {
287
+
288
+ if (token.type === 'paragraph_open' && nextToken.type === 'inline' && nextToken.children[0].type === 'image' && state.tokens[n+2].type === 'paragraph_close' && nextToken.children.length < 3) {
289
+ if (nextToken.children.length === 2) {
290
+ if (!nextToken.children[nextToken.children.length - 1].type === 'text' || !/^ *?\{.*?\}$/.test(nextToken.children[nextToken.children.length - 1].content)) {
291
+ n++; continue;
292
+ }
293
+ }
284
294
  checkToken = true;
285
295
  en = n + 2;
286
296
  range.end = en;
@@ -288,9 +298,9 @@ module.exports = function figure_with_caption_plugin(md, option) {
288
298
  nextToken.children[0].type = 'image';
289
299
  caption = checkCaption(state, n, en, tagName, caption);
290
300
  if (opt.oneImageWithoutCaption && state.tokens[n-1]) {
291
- if (state.tokens[n-1].type === 'list_item_open') {n++; continue;}
301
+ if (state.tokens[n-1].type === 'list_item_open') {checkToken = false;}
292
302
  }
293
- if (opt.oneImageWithoutCaption || caption.hasPrev || caption.hasNext) {
303
+ if (checkToken && (opt.oneImageWithoutCaption || caption.hasPrev || caption.hasNext)) {
294
304
  range = wrapWithFigure(state, range, tagName, true);
295
305
  }
296
306
  }
@@ -322,6 +332,7 @@ module.exports = function figure_with_caption_plugin(md, option) {
322
332
  bLabel: opt.bLabel,
323
333
  strongLabel: opt.strongLabel,
324
334
  jointSpaceUseHalfWidth: opt.jointSpaceUseHalfWidth,
335
+ removeUnnumberedLabel: opt.removeUnnumberedLabel,
325
336
  });
326
337
  md.core.ruler.before('linkify', 'figure_with_caption', figureWithCaption);
327
338
  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.2",
3
+ "version": "0.4.4",
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,9 +13,10 @@
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
- "p7d-markdown-it-p-captions": "^0.8.0"
20
+ "p7d-markdown-it-p-captions": "^0.9.0"
20
21
  }
21
22
  }
package/test/examples.txt CHANGED
@@ -372,9 +372,16 @@ A paragraph.
372
372
  </figure>
373
373
  <p>A paragraph.</p>
374
374
 
375
+
375
376
  [Markdown]
376
377
  A paragraph.
377
378
 
379
+ Figure. A cat.
380
+
381
+ ![Figure](cat.jpg)
382
+
383
+ A paragraph.
384
+
378
385
  Video. A mp4.
379
386
 
380
387
  <video controls width="400" height="300">
@@ -382,8 +389,14 @@ Video. A mp4.
382
389
  </video>
383
390
 
384
391
  A paragraph.
392
+
385
393
  [HTML]
386
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>
387
400
  <figure class="f-video">
388
401
  <figcaption><span class="f-video-label">Video<span class="f-video-label-joint">.</span></span> A mp4.</figcaption>
389
402
  <video controls width="400" height="300">
@@ -392,6 +405,37 @@ A paragraph.
392
405
  </figure>
393
406
  <p>A paragraph.</p>
394
407
 
408
+
409
+ [Markdown]
410
+ A paragraph.
411
+
412
+ Figure. A cat.
413
+
414
+ ![Figure](cat.jpg)
415
+
416
+ Video. A mp4.
417
+
418
+ <video controls width="400" height="300">
419
+ <source src="example.mp4" type="video/mp4">
420
+ </video>
421
+
422
+ A paragraph.
423
+
424
+ [HTML]
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>
430
+ <figure class="f-video">
431
+ <figcaption><span class="f-video-label">Video<span class="f-video-label-joint">.</span></span> A mp4.</figcaption>
432
+ <video controls width="400" height="300">
433
+ <source src="example.mp4" type="video/mp4">
434
+ </video>
435
+ </figure>
436
+ <p>A paragraph.</p>
437
+
438
+
395
439
  [Markdown]
396
440
  A paragraph.
397
441
 
@@ -621,3 +665,21 @@ A paragraph.
621
665
  </figure>
622
666
  <p>A paragraph.</p>
623
667
 
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>
package/test/test.js CHANGED
@@ -1,18 +1,24 @@
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
 
7
+ const attrs = require('../node_modules/markdown-it-attrs');
8
8
 
9
9
  md.use(mdFigureWithPCaption, {
10
10
  dquoteFilename: true,
11
11
  strongFilename: true,
12
- oneImageWithoutCaption: true,
12
+ oneImageWithoutCaption: false,
13
13
  hasNumClass: true,
14
14
  });
15
15
 
16
+ mdOneImage.use(mdFigureWithPCaption, {
17
+ dquoteFilename: true,
18
+ strongFilename: true,
19
+ oneImageWithoutCaption: true,
20
+ hasNumClass: true,
21
+ }).use(attrs);
16
22
 
17
23
  const example = __dirname + '/examples.txt';
18
24
  const mdPath = __dirname + '/examples.md';
@@ -40,12 +46,18 @@ while(n < ms0.length) {
40
46
 
41
47
  n = 1;
42
48
  while(n < ms.length) {
43
- //if (n !== 24) { 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) {