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

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,8 @@ module.exports = function figure_with_caption_plugin(md, option) {
16
16
  oneImageWithoutCaption: false,
17
17
  iframeWithoutCaption: false,
18
18
  removeUnnumberedLabel: false,
19
+ multipleImages: true,
20
+ styleProcess: true,
19
21
  };
20
22
  if (option !== undefined) {
21
23
  for (let o in option) {
@@ -141,6 +143,13 @@ module.exports = function figure_with_caption_plugin(md, option) {
141
143
  const figureEndToken = new state.Token('figure_close', 'figure', -1);
142
144
  const breakToken = new state.Token('text', '', 0);
143
145
  breakToken.content = '\n';
146
+ if (sp) {
147
+ if (sp.attrs) {
148
+ for (let attr of sp.attrs) {
149
+ figureStartToken.attrJoin(attr[0], attr[1]);
150
+ }
151
+ }
152
+ }
144
153
  ///Add for vsce
145
154
  if(state.tokens[n].attrs) {
146
155
  for (let attr of state.tokens[n].attrs) {
@@ -186,9 +195,11 @@ module.exports = function figure_with_caption_plugin(md, option) {
186
195
  let tagName = '';
187
196
  let caption = {
188
197
  name: '',
198
+ nameSuffix: '',
189
199
  hasPrev: false,
190
200
  hasNext: false,
191
201
  };
202
+ let sp = {attrs: []};
192
203
 
193
204
  const checkTags = ['table', 'pre', 'blockquote'];
194
205
  let cti = 0;
@@ -239,7 +250,6 @@ module.exports = function figure_with_caption_plugin(md, option) {
239
250
  if (token.type === 'html_block') {
240
251
  const tags = ['video', 'audio', 'iframe', 'blockquote'];
241
252
  let ctj = 0;
242
- let sp = {};
243
253
  while (ctj < tags.length) {
244
254
  const hasTag = token.content.match(new RegExp('^<'+ tags[ctj] + ' ?[^>]*?>[\\s\\S]*?<\\/' + tags[ctj] + '> *?(?:<script [^>]*?>(?:</script>)?)?(\\n|$)'));
245
255
  if (!hasTag) {
@@ -301,13 +311,77 @@ module.exports = function figure_with_caption_plugin(md, option) {
301
311
  }
302
312
 
303
313
 
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;
314
+ if (token.type === 'paragraph_open' && nextToken.type === 'inline' && nextToken.children[0].type === 'image') {
315
+ let ntChildTokenIndex = 1
316
+ let imageNum = 1
317
+ let isMultipleImagesHorizontal = true
318
+ let isMultipleImagesVertical = true
319
+ checkToken = true
320
+ while (ntChildTokenIndex < nextToken.children.length) {
321
+ const ntChildToken = nextToken.children[ntChildTokenIndex]
322
+ if (ntChildTokenIndex === nextToken.children.length - 1) {
323
+ let imageAttrs = ntChildToken.content.match(/^ *\{(.*?)\} *$/)
324
+ if(ntChildToken.type === 'text' && imageAttrs) {
325
+ imageAttrs = imageAttrs[1].split(/ +/)
326
+ let iai = 0
327
+ while (iai < imageAttrs.length) {
328
+ if (/^\./.test(imageAttrs[iai])) {
329
+ imageAttrs[iai] = imageAttrs[iai].replace(/^\./, "class=")
330
+ }
331
+ if (/^#/.test(imageAttrs[iai])) {
332
+ imageAttrs[iai] = imageAttrs[iai].replace(/^\#/, "id=")
333
+ }
334
+ let imageAttr = imageAttrs[iai].match(/^(.*?)="?(.*)"?$/)
335
+ if (!imageAttr || !imageAttr[1]) {
336
+ iai++
337
+ continue
338
+ }
339
+ sp.attrs.push([imageAttr[1], imageAttr[2]])
340
+ iai++
341
+ }
342
+ break
343
+ }
344
+ }
345
+
346
+ if (!opt.multipleImages) {
347
+ checkToken = false
348
+ break
349
+ }
350
+ if (ntChildToken.type === 'image') {
351
+ imageNum += 1
352
+ } else if (ntChildToken.type === 'text' && /^ *$/.test(ntChildToken.content)) {
353
+ isMultipleImagesVertical = false
354
+ if (isMultipleImagesVertical) {
355
+ isMultipleImagesHorizontal = false
356
+ }
357
+ } else if (ntChildToken.type === 'softbreak') {
358
+ isMultipleImagesHorizontal = false
359
+ if (isMultipleImagesHorizontal) {
360
+ isMultipleImagesVertical = false
361
+ }
362
+ } else {
363
+ checkToken = false
364
+ break
365
+ }
366
+ ntChildTokenIndex++
367
+ }
368
+ if (checkToken && imageNum > 1 && opt.multipleImages) {
369
+ if (isMultipleImagesHorizontal) {
370
+ caption.nameSuffix = '-horizontal'
371
+ } else if (isMultipleImagesVertical) {
372
+ caption.nameSuffix = '-vertical'
373
+ } else {
374
+ caption.nameSuffix = '-multiple'
375
+ }
376
+ ntChildTokenIndex = 0
377
+ while (ntChildTokenIndex < nextToken.children.length) {
378
+ const ccToken = nextToken.children[ntChildTokenIndex]
379
+ if (ccToken.type === 'text' && /^ *$/.test(ccToken.content)) {
380
+ ccToken.content = ''
381
+ }
382
+ ntChildTokenIndex++
308
383
  }
309
384
  }
310
- checkToken = true;
311
385
  en = n + 2;
312
386
  range.end = en;
313
387
  tagName = 'img';
@@ -317,7 +391,12 @@ module.exports = function figure_with_caption_plugin(md, option) {
317
391
  if (state.tokens[n-1].type === 'list_item_open') {checkToken = false;}
318
392
  }
319
393
  if (checkToken && (opt.oneImageWithoutCaption || caption.hasPrev || caption.hasNext)) {
320
- range = wrapWithFigure(state, range, tagName, true);
394
+ if (caption.nameSuffix) tagName += caption.nameSuffix
395
+ if (caption.hasNext && opt.styleProcess) {
396
+ range = wrapWithFigure(state, range, tagName, true, sp);
397
+ } else {
398
+ range = wrapWithFigure(state, range, tagName, true);
399
+ }
321
400
  }
322
401
  }
323
402
 
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.0",
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": {
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);