@peaceroad/markdown-it-figure-with-p-caption 0.4.6 → 0.5.1

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
@@ -2,7 +2,7 @@
2
2
 
3
3
  This is a markdown-it plugin.
4
4
 
5
- For a paragraph with only one image, a table or code block or a blockquote, and by writing a caption paragraph immediately before or after, they are converted into the figure element with the figcaption element.
5
+ For a paragraph with one image/images only, a table or code block or a blockquote, and by writing a caption paragraph immediately before or after, they are converted into the figure element with the figcaption element.
6
6
 
7
7
  1. Check that the element: one image only paragraph, table, code block, samp block, blockquote, and video.
8
8
  2. Check if this element has a caption paragraph immediately before or after it
@@ -10,7 +10,9 @@ 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.
13
+ Notice. If code block language setting is "samp", change it to use samp element instead of code element.
14
+
15
+ Notice. It assumes simultaneous use of `markdown-it-attrs`. However, if there is `{.style}` at the end of the image-only paragraph, and the next paragraph is a caption, processing is not handled well with `markdown-it-attrs` alone, so in order to normalize it, {} The processing is written in this plugin. (This process can be turned off by specifying `{styleProcess: false}`.) [0.5.0]
14
16
 
15
17
  Use it as follows.
16
18
 
@@ -96,7 +98,6 @@ A paragraph.
96
98
  <p>A paragraph.</p>
97
99
 
98
100
 
99
-
100
101
  [Markdown]
101
102
  A paragraph.
102
103
 
@@ -237,6 +238,68 @@ A paragraph.
237
238
  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.
238
239
 
239
240
 
241
+ From version 0.5.0, it supports cases where a paragraph contains only multiple images. Instead of `f-img` as the figure class name, use the following class name. (This class name is unstable, but I probably won't change it.)
242
+
243
+ - `f-img-horizontal` if the image is written in one line on Markdown
244
+ - `f-img-vertical` if images are written only vertically, one per line
245
+ - `f-img-multiple` in other cases
246
+
247
+ Notice. This process can be turned off by specifying `{multipleImages: false}`.
248
+
249
+ ```
250
+ [Markdown]
251
+ A paragraph. multipleImages: true. horizontal images only.
252
+
253
+ ![One cat](cat1.jpg) ![Two cat](cat2.jpg)
254
+
255
+ Figure. Cats.
256
+
257
+ A paragraph.
258
+ [HTML]
259
+ <p>A paragraph. multipleImages: true. horizontal images only</p>
260
+ <figure class="f-img-horizontal">
261
+ <img src="cat1.jpg" alt="One cat"><img src="cat2.jpg" alt="Two cat">
262
+ <figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> Cats.</figcaption>
263
+ </figure>
264
+ <p>A paragraph.</p>
265
+
266
+ [Markdown]
267
+ A paragraph. multipleImages: true. vertical images only.
268
+
269
+ Figure. Cats.
270
+
271
+ ![One cat](cat1.jpg)
272
+ ![Two cat](cat2.jpg)
273
+
274
+ A paragraph.
275
+ [HTML]
276
+ <p>A paragraph. multipleImages: true. vertical images only.</p>
277
+ <figure class="f-img-vertical">
278
+ <figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> Cats.</figcaption>
279
+ <img src="cat1.jpg" alt="One cat">
280
+ <img src="cat2.jpg" alt="Two cat">
281
+ </figure>
282
+ <p>A paragraph.</p>
283
+
284
+ [Markdown]
285
+ A paragraph. multipleImages: true.
286
+
287
+ Figure. Cats.
288
+
289
+ ![One cat](cat1.jpg) ![Two cat](cat2.jpg)
290
+ ![Three cat](cat3.jpg)
291
+
292
+ A paragraph.
293
+ [HTML]
294
+ <p>A paragraph. multipleImages: true.</p>
295
+ <figure class="f-img-multiple">
296
+ <figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> Cats.</figcaption>
297
+ <img src="cat1.jpg" alt="One cat"><img src="cat2.jpg" alt="Two cat">
298
+ <img src="cat3.jpg" alt="Three cat">
299
+ </figure>
300
+ <p>A paragraph.</p>
301
+ ```
302
+
240
303
  ## Option: Specify file name
241
304
 
242
305
  Specify the file name before writing the caption.
package/index.js CHANGED
@@ -16,6 +16,9 @@ module.exports = function figure_with_caption_plugin(md, option) {
16
16
  oneImageWithoutCaption: false,
17
17
  iframeWithoutCaption: false,
18
18
  removeUnnumberedLabel: false,
19
+ removeUnnumberedLabelExceptMarks: [],
20
+ multipleImages: true,
21
+ styleProcess: true,
19
22
  };
20
23
  if (option !== undefined) {
21
24
  for (let o in option) {
@@ -141,6 +144,13 @@ module.exports = function figure_with_caption_plugin(md, option) {
141
144
  const figureEndToken = new state.Token('figure_close', 'figure', -1);
142
145
  const breakToken = new state.Token('text', '', 0);
143
146
  breakToken.content = '\n';
147
+ if (sp) {
148
+ if (sp.attrs) {
149
+ for (let attr of sp.attrs) {
150
+ figureStartToken.attrJoin(attr[0], attr[1]);
151
+ }
152
+ }
153
+ }
144
154
  ///Add for vsce
145
155
  if(state.tokens[n].attrs) {
146
156
  for (let attr of state.tokens[n].attrs) {
@@ -186,9 +196,11 @@ module.exports = function figure_with_caption_plugin(md, option) {
186
196
  let tagName = '';
187
197
  let caption = {
188
198
  name: '',
199
+ nameSuffix: '',
189
200
  hasPrev: false,
190
201
  hasNext: false,
191
202
  };
203
+ let sp = {attrs: []};
192
204
 
193
205
  const checkTags = ['table', 'pre', 'blockquote'];
194
206
  let cti = 0;
@@ -239,7 +251,6 @@ module.exports = function figure_with_caption_plugin(md, option) {
239
251
  if (token.type === 'html_block') {
240
252
  const tags = ['video', 'audio', 'iframe', 'blockquote'];
241
253
  let ctj = 0;
242
- let sp = {};
243
254
  while (ctj < tags.length) {
244
255
  const hasTag = token.content.match(new RegExp('^<'+ tags[ctj] + ' ?[^>]*?>[\\s\\S]*?<\\/' + tags[ctj] + '> *?(?:<script [^>]*?>(?:</script>)?)?(\\n|$)'));
245
256
  if (!hasTag) {
@@ -301,13 +312,77 @@ module.exports = function figure_with_caption_plugin(md, option) {
301
312
  }
302
313
 
303
314
 
304
- if (token.type === 'paragraph_open' && nextToken.type === 'inline' && nextToken.children[0].type === 'image' && state.tokens[n+2].type === 'paragraph_close' && nextToken.children.length < 3) {
305
- if (nextToken.children.length === 2) {
306
- if (!nextToken.children[nextToken.children.length - 1].type === 'text' || !/^ *?\{.*?\}$/.test(nextToken.children[nextToken.children.length - 1].content)) {
307
- n++; continue;
315
+ if (token.type === 'paragraph_open' && nextToken.type === 'inline' && nextToken.children[0].type === 'image') {
316
+ let ntChildTokenIndex = 1
317
+ let imageNum = 1
318
+ let isMultipleImagesHorizontal = true
319
+ let isMultipleImagesVertical = true
320
+ checkToken = true
321
+ while (ntChildTokenIndex < nextToken.children.length) {
322
+ const ntChildToken = nextToken.children[ntChildTokenIndex]
323
+ if (ntChildTokenIndex === nextToken.children.length - 1) {
324
+ let imageAttrs = ntChildToken.content.match(/^ *\{(.*?)\} *$/)
325
+ if(ntChildToken.type === 'text' && imageAttrs) {
326
+ imageAttrs = imageAttrs[1].split(/ +/)
327
+ let iai = 0
328
+ while (iai < imageAttrs.length) {
329
+ if (/^\./.test(imageAttrs[iai])) {
330
+ imageAttrs[iai] = imageAttrs[iai].replace(/^\./, "class=")
331
+ }
332
+ if (/^#/.test(imageAttrs[iai])) {
333
+ imageAttrs[iai] = imageAttrs[iai].replace(/^\#/, "id=")
334
+ }
335
+ let imageAttr = imageAttrs[iai].match(/^(.*?)="?(.*)"?$/)
336
+ if (!imageAttr || !imageAttr[1]) {
337
+ iai++
338
+ continue
339
+ }
340
+ sp.attrs.push([imageAttr[1], imageAttr[2]])
341
+ iai++
342
+ }
343
+ break
344
+ }
345
+ }
346
+
347
+ if (!opt.multipleImages) {
348
+ checkToken = false
349
+ break
350
+ }
351
+ if (ntChildToken.type === 'image') {
352
+ imageNum += 1
353
+ } else if (ntChildToken.type === 'text' && /^ *$/.test(ntChildToken.content)) {
354
+ isMultipleImagesVertical = false
355
+ if (isMultipleImagesVertical) {
356
+ isMultipleImagesHorizontal = false
357
+ }
358
+ } else if (ntChildToken.type === 'softbreak') {
359
+ isMultipleImagesHorizontal = false
360
+ if (isMultipleImagesHorizontal) {
361
+ isMultipleImagesVertical = false
362
+ }
363
+ } else {
364
+ checkToken = false
365
+ break
366
+ }
367
+ ntChildTokenIndex++
368
+ }
369
+ if (checkToken && imageNum > 1 && opt.multipleImages) {
370
+ if (isMultipleImagesHorizontal) {
371
+ caption.nameSuffix = '-horizontal'
372
+ } else if (isMultipleImagesVertical) {
373
+ caption.nameSuffix = '-vertical'
374
+ } else {
375
+ caption.nameSuffix = '-multiple'
376
+ }
377
+ ntChildTokenIndex = 0
378
+ while (ntChildTokenIndex < nextToken.children.length) {
379
+ const ccToken = nextToken.children[ntChildTokenIndex]
380
+ if (ccToken.type === 'text' && /^ *$/.test(ccToken.content)) {
381
+ ccToken.content = ''
382
+ }
383
+ ntChildTokenIndex++
308
384
  }
309
385
  }
310
- checkToken = true;
311
386
  en = n + 2;
312
387
  range.end = en;
313
388
  tagName = 'img';
@@ -317,7 +392,12 @@ module.exports = function figure_with_caption_plugin(md, option) {
317
392
  if (state.tokens[n-1].type === 'list_item_open') {checkToken = false;}
318
393
  }
319
394
  if (checkToken && (opt.oneImageWithoutCaption || caption.hasPrev || caption.hasNext)) {
320
- range = wrapWithFigure(state, range, tagName, true);
395
+ if (caption.nameSuffix) tagName += caption.nameSuffix
396
+ if (caption.hasNext && opt.styleProcess) {
397
+ range = wrapWithFigure(state, range, tagName, true, sp);
398
+ } else {
399
+ range = wrapWithFigure(state, range, tagName, true);
400
+ }
321
401
  }
322
402
  }
323
403
 
@@ -349,6 +429,7 @@ module.exports = function figure_with_caption_plugin(md, option) {
349
429
  strongLabel: opt.strongLabel,
350
430
  jointSpaceUseHalfWidth: opt.jointSpaceUseHalfWidth,
351
431
  removeUnnumberedLabel: opt.removeUnnumberedLabel,
432
+ removeUnnumberedLabelExceptMarks: opt.removeUnnumberedLabelExceptMarks,
352
433
  });
353
434
  md.core.ruler.before('linkify', 'figure_with_caption', figureWithCaption);
354
435
  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.6",
3
+ "version": "0.5.1",
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": {
@@ -17,6 +17,6 @@
17
17
  "markdown-it-attrs": "^4.1.4"
18
18
  },
19
19
  "dependencies": {
20
- "p7d-markdown-it-p-captions": "^0.9.1"
20
+ "p7d-markdown-it-p-captions": "^0.10.0"
21
21
  }
22
22
  }
package/test/examples.txt CHANGED
@@ -676,6 +676,17 @@ Figure. A Caption.
676
676
  <img src="cat.jpg" alt="Figure">
677
677
  </figure>
678
678
 
679
+
680
+ [Markdown]
681
+ ![Figure](cat.jpg) {.style}
682
+
683
+ Figure. A Caption.
684
+ [HTML]
685
+ <figure class="f-img style">
686
+ <img src="cat.jpg" alt="Figure">
687
+ <figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> A Caption.</figcaption>
688
+ </figure>
689
+
679
690
  [Markdown]
680
691
  Figure. A Caption.
681
692
 
@@ -748,3 +759,142 @@ A paragraph.
748
759
  </figure>
749
760
  <p>A paragraph.</p>
750
761
 
762
+ [Markdown]
763
+ A paragraph. multipleImages: true.
764
+
765
+ Figure. Cats.
766
+
767
+ ![One cat](cat1.jpg) ![Two cat](cat2.jpg)
768
+
769
+ A paragraph.
770
+ [HTML]
771
+ <p>A paragraph. multipleImages: true.</p>
772
+ <figure class="f-img-horizontal">
773
+ <figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> Cats.</figcaption>
774
+ <img src="cat1.jpg" alt="One cat"><img src="cat2.jpg" alt="Two cat">
775
+ </figure>
776
+ <p>A paragraph.</p>
777
+
778
+
779
+ [Markdown]
780
+ A paragraph. multipleImages: true.
781
+
782
+ ![One cat](cat1.jpg) ![Two cat](cat2.jpg)
783
+
784
+ Figure. Cats.
785
+
786
+ A paragraph.
787
+ [HTML]
788
+ <p>A paragraph. multipleImages: true.</p>
789
+ <figure class="f-img-horizontal">
790
+ <img src="cat1.jpg" alt="One cat"><img src="cat2.jpg" alt="Two cat">
791
+ <figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> Cats.</figcaption>
792
+ </figure>
793
+ <p>A paragraph.</p>
794
+
795
+
796
+ [Markdown]
797
+ A paragraph. multipleImages: true.
798
+
799
+ Figure. Cats.
800
+
801
+ ![One cat](cat1.jpg)![Two cat](cat2.jpg)![Three cat](cat3.jpg) {.style}
802
+
803
+ A paragraph.
804
+ [HTML]
805
+ <p>A paragraph. multipleImages: true.</p>
806
+ <figure class="f-img-horizontal style">
807
+ <figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> Cats.</figcaption>
808
+ <img src="cat1.jpg" alt="One cat"><img src="cat2.jpg" alt="Two cat"><img src="cat3.jpg" alt="Three cat">
809
+ </figure>
810
+ <p>A paragraph.</p>
811
+
812
+ [Markdown]
813
+ A paragraph. multipleImages: true.
814
+
815
+ Figure. Cats.
816
+
817
+ ![One cat](cat1.jpg)
818
+ ![Two cat](cat2.jpg)
819
+
820
+ A paragraph.
821
+ [HTML]
822
+ <p>A paragraph. multipleImages: true.</p>
823
+ <figure class="f-img-vertical">
824
+ <figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> Cats.</figcaption>
825
+ <img src="cat1.jpg" alt="One cat">
826
+ <img src="cat2.jpg" alt="Two cat">
827
+ </figure>
828
+ <p>A paragraph.</p>
829
+
830
+ [Markdown]
831
+ A paragraph. multipleImages: true.
832
+
833
+ Figure. Cats.
834
+
835
+ ![One cat](cat1.jpg) ![Two cat](cat2.jpg)
836
+ ![Three cat](cat3.jpg)
837
+
838
+ A paragraph.
839
+ [HTML]
840
+ <p>A paragraph. multipleImages: true.</p>
841
+ <figure class="f-img-multiple">
842
+ <figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> Cats.</figcaption>
843
+ <img src="cat1.jpg" alt="One cat"><img src="cat2.jpg" alt="Two cat">
844
+ <img src="cat3.jpg" alt="Three cat">
845
+ </figure>
846
+ <p>A paragraph.</p>
847
+
848
+ [Markdown]
849
+ A paragraph. multipleImages: true.
850
+
851
+ Figure. Cats.
852
+
853
+ ![One cat](cat1.jpg) ![Two cat](cat2.jpg)
854
+ ![Three cat](cat3.jpg) {.style}
855
+
856
+ A paragraph.
857
+ [HTML]
858
+ <p>A paragraph. multipleImages: true.</p>
859
+ <figure class="f-img-multiple style">
860
+ <figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> Cats.</figcaption>
861
+ <img src="cat1.jpg" alt="One cat"><img src="cat2.jpg" alt="Two cat">
862
+ <img src="cat3.jpg" alt="Three cat">
863
+ </figure>
864
+ <p>A paragraph.</p>
865
+
866
+
867
+ [Markdown]
868
+ A paragraph. multipleImages: true.
869
+
870
+ ![One cat](cat1.jpg) ![Two cat](cat2.jpg)
871
+ ![Three cat](cat3.jpg)![Four cat](cat4.jpg) {.style #id}
872
+
873
+ Figure. Cats.
874
+
875
+ A paragraph.
876
+ [HTML]
877
+ <p>A paragraph. multipleImages: true.</p>
878
+ <figure class="f-img-multiple style" id="id">
879
+ <img src="cat1.jpg" alt="One cat"><img src="cat2.jpg" alt="Two cat">
880
+ <img src="cat3.jpg" alt="Three cat"><img src="cat4.jpg" alt="Four cat">
881
+ <figcaption><span class="f-img-label">Figure<span class="f-img-label-joint">.</span></span> Cats.</figcaption>
882
+ </figure>
883
+ <p>A paragraph.</p>
884
+
885
+
886
+
887
+ [Markdown]
888
+ A paragraph. multipleImages: true.
889
+
890
+ ![One cat](cat1.jpg) ![Two cat](cat2.jpg)
891
+ ![Three cat](cat3.jpg)![Four cat](cat4.jpg) {.style #id}
892
+
893
+ A paragraph.
894
+ [HTML]
895
+ <p>A paragraph. multipleImages: true.</p>
896
+ <figure class="f-img-multiple style" id="id">
897
+ <img src="cat1.jpg" alt="One cat"><img src="cat2.jpg" alt="Two cat">
898
+ <img src="cat3.jpg" alt="Three cat"><img src="cat4.jpg" alt="Four cat">
899
+ </figure>
900
+ <p>A paragraph.</p>
package/test/test.js CHANGED
@@ -3,6 +3,7 @@ const fs = require('fs');
3
3
  const md = require('markdown-it')({ html: true });
4
4
  const mdOneImage = require('markdown-it')({ html: true });
5
5
  const mdWithoutCaption = require('markdown-it')({ html: true });
6
+ const mdMultipleImages = require('markdown-it')({ html: true });
6
7
 
7
8
  const mdFigureWithPCaption = require('../index.js');
8
9
 
@@ -30,6 +31,15 @@ mdWithoutCaption.use(mdFigureWithPCaption, {
30
31
  hasNumClass: true,
31
32
  }).use(attrs);
32
33
 
34
+ mdMultipleImages.use(attrs).use(mdFigureWithPCaption, {
35
+ dquoteFilename: true,
36
+ strongFilename: true,
37
+ oneImageWithoutCaption: true,
38
+ iframeWithoutCaption: true,
39
+ hasNumClass: true,
40
+ multipleImages: true,
41
+ })
42
+
33
43
  const example = __dirname + '/examples.txt';
34
44
  const mdPath = __dirname + '/examples.md';
35
45
  const exampleCont = fs.readFileSync(example, 'utf-8').trim();
@@ -56,13 +66,15 @@ while(n < ms0.length) {
56
66
 
57
67
  n = 1;
58
68
  while(n < ms.length) {
59
- //if (n !== 22) { n++; continue };
69
+ //if (n !== 37) { n++; continue };
60
70
  console.log('Test: ' + n + ' >>>');
61
71
  //console.log(ms[n].markdown);
62
72
 
63
73
  const m = ms[n].markdown;
64
74
  let h = ''
65
- if (n > 37) {
75
+ if (n > 44) {
76
+ h = mdMultipleImages.render(m);
77
+ } else if (n > 37) {
66
78
  h = mdWithoutCaption.render(m);
67
79
  } else if (n > 20) {
68
80
  h = mdOneImage.render(m);