@peaceroad/markdown-it-figure-with-p-caption 0.3.1 → 0.4.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
@@ -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,4 +159,129 @@ 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
+ [Markdown]
185
+ A paragraph.
186
+
187
+ Video. A youtube.
188
+
189
+ <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>
190
+
191
+ A paragraph.
192
+ [HTML]
193
+ <p>A paragraph.</p>
194
+ <figure class="f-video">
195
+ <figcaption><span class="f-video-label">Video<span class="f-video-label-joint">.</span></span> A youtube.</figcaption>
196
+ <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
+ </figure>
198
+ <p>A paragraph.</p>
199
+ ~~~
200
+
201
+
202
+ ## Option: Specify file name
203
+
204
+ Specify the file name before writing the caption.
205
+ Note that a space is required between the file name and caption.
206
+
207
+ ### Use double quote
208
+
209
+ ```js
210
+
211
+ md.use(mdFigureWithPCaption, {dquoteFilename: true});
212
+ ```
213
+
160
214
  ~~~
215
+ [Markdown]
216
+ A paragraph.
217
+
218
+ Code. "filename.js" Call a cat.
219
+
220
+ ```js
221
+ console.log('Nyaan!');
222
+ ```
223
+
224
+ A paragraph.
225
+
226
+ [HTML]
227
+ <p>A paragraph.</p>
228
+ <figure class="f-pre-code" role="doc-example">
229
+ <figcaption><span class="f-pre-code-label">Code<span class="f-pre-code-label-joint">.</span></span> <strong class="f-pre-code-filename">filename.js</strong> Call a cat.</figcaption>
230
+ <pre><code class="language-js">console.log('Nyaan!');
231
+ </code></pre>
232
+ </figure>
233
+ <p>A paragraph.</p>
234
+ ~~~
235
+
236
+ ### Use strong mark
237
+
238
+ ```js
239
+ md.use(mdFigureWithPCaption, {strongFilename: true});
240
+ ```
241
+
242
+ ~~~
243
+
244
+ [Markdown]
245
+ A paragraph.
246
+
247
+ Code. **filename.js** Call a cat.
248
+
249
+ ```js
250
+ console.log('Nyaan!');
251
+ ```
252
+
253
+ A paragraph.
254
+
255
+ [HTML]
256
+ <p>A paragraph.</p>
257
+ <figure class="f-pre-code" role="doc-example">
258
+ <figcaption><span class="f-pre-code-label">Code<span class="f-pre-code-label-joint">.</span></span> <strong class="f-pre-code-filename">filename.js</strong> Call a cat.</figcaption>
259
+ <pre><code class="language-js">console.log('Nyaan!');
260
+ </code></pre>
261
+ </figure>
262
+ <p>A paragraph.</p>
263
+ ~~~
264
+
265
+ ## Option: Convert one image paragraph without caption
266
+
267
+ Convert one image paragraph without a caption paragraph to figure element.
268
+
269
+ ```js
270
+ md.use(mdFigureWithPCaption, {oneImageWithoutCaption: true});
271
+ ```
272
+
273
+ ~~~
274
+ [Markdown]
275
+ A paragraph.
276
+
277
+ ![Figure](cat.jpg)
278
+
279
+ A paragraph.
280
+
281
+ [HTML]
282
+ <p>A paragraph.</p>
283
+ <figure class="f-img">
284
+ <img src="cat.jpg" alt="Figure">
285
+ </figure>
286
+ <p>A paragraph.</p>
287
+ ~~~
package/index.js CHANGED
@@ -7,6 +7,9 @@ module.exports = function figure_with_caption_plugin(md, option) {
7
7
  let opt = {
8
8
  classPrefix: 'f',
9
9
  scaleSuffix: false,
10
+ dquoteFilename: false,
11
+ strongFilename: false,
12
+ oneImageWithoutCaption: false,
10
13
  };
11
14
  if (option !== undefined) {
12
15
  for (let o in option) {
@@ -215,7 +218,7 @@ module.exports = function figure_with_caption_plugin(md, option) {
215
218
  const tags = ['video', 'audio', 'iframe'];
216
219
  let ctj = 0;
217
220
  while (ctj < tags.length) {
218
- const hasTag = token.content.match(new RegExp('^<'+ tags[ctj] + ' ?[^>]*?>[\\s\\S]*?<\\/' + tags[ctj] + '>\\n'))
221
+ const hasTag = token.content.match(new RegExp('^<'+ tags[ctj] + ' ?[^>]*?>[\\s\\S]*?<\\/' + tags[ctj] + '>\\n'));
219
222
  if (!hasTag) {
220
223
  ctj++;
221
224
  continue;
@@ -232,8 +235,8 @@ module.exports = function figure_with_caption_plugin(md, option) {
232
235
  range = wrapWithFigure(state, range, tagName, false);
233
236
  break;
234
237
  }
238
+ ctj++
235
239
  }
236
- ctj++
237
240
  }
238
241
 
239
242
  if (token.type === 'paragraph_open'
@@ -247,7 +250,7 @@ module.exports = function figure_with_caption_plugin(md, option) {
247
250
  tagName = 'img';
248
251
  nextToken.children[0].type = 'image';
249
252
  caption = checkCaption(state, n, en, tagName, caption);
250
- if (caption.hasPrev || caption.hasNext) {
253
+ if (opt.oneImageWithoutCaption || caption.hasPrev || caption.hasNext) {
251
254
  range = wrapWithFigure(state, range, tagName, true);
252
255
  }
253
256
  }
@@ -271,7 +274,11 @@ module.exports = function figure_with_caption_plugin(md, option) {
271
274
  return;
272
275
  }
273
276
 
274
- md.use(mdPCaption, {'classPrefix': opt.classPrefix});
277
+ md.use(mdPCaption, {
278
+ classPrefix: opt.classPrefix,
279
+ dquoteFilename: opt.dquoteFilename,
280
+ strongFilename: opt.strongFilename,
281
+ });
275
282
  md.core.ruler.before('linkify', 'figure_with_caption', figureWithCaption);
276
283
  md.renderer.rules['fence_samp'] = function (tokens, idx, options, env, slf) {
277
284
  const token = tokens[idx];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peaceroad/markdown-it-figure-with-p-caption",
3
- "version": "0.3.1",
3
+ "version": "0.4.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": {
@@ -16,6 +16,6 @@
16
16
  "markdown-it": "^12.1.0"
17
17
  },
18
18
  "dependencies": {
19
- "p7d-markdown-it-p-captions": "^0.5.0"
19
+ "p7d-markdown-it-p-captions": "^0.6.3"
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
 
@@ -408,3 +407,103 @@ A paragraph.
408
407
  <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>
409
408
  </figure>
410
409
  <p>A paragraph.</p>
410
+
411
+
412
+ [Markdown]
413
+ A paragraph.
414
+
415
+ Code. "filename.js" Call a cat.
416
+
417
+ ```js
418
+ console.log('Nyaan!');
419
+ ```
420
+
421
+ A paragraph.
422
+
423
+ [HTML]
424
+ <p>A paragraph.</p>
425
+ <figure class="f-pre-code" role="doc-example">
426
+ <figcaption><span class="f-pre-code-label">Code<span class="f-pre-code-label-joint">.</span></span> <strong class="f-pre-code-filename">filename.js</strong> Call a cat.</figcaption>
427
+ <pre><code class="language-js">console.log('Nyaan!');
428
+ </code></pre>
429
+ </figure>
430
+ <p>A paragraph.</p>
431
+
432
+ [Markdown]
433
+ A paragraph.
434
+
435
+ Code. **filename.js** Call a cat.
436
+
437
+ ```js
438
+ console.log('Nyaan!');
439
+ ```
440
+
441
+ A paragraph.
442
+
443
+ [HTML]
444
+ <p>A paragraph.</p>
445
+ <figure class="f-pre-code" role="doc-example">
446
+ <figcaption><span class="f-pre-code-label">Code<span class="f-pre-code-label-joint">.</span></span> <strong class="f-pre-code-filename">filename.js</strong> Call a cat.</figcaption>
447
+ <pre><code class="language-js">console.log('Nyaan!');
448
+ </code></pre>
449
+ </figure>
450
+ <p>A paragraph.</p>
451
+
452
+ [Markdown]
453
+ A paragraph.
454
+
455
+ Code. "filename.js"
456
+
457
+ ```js
458
+ console.log('Nyaan!');
459
+ ```
460
+
461
+ A paragraph.
462
+
463
+ [HTML]
464
+ <p>A paragraph.</p>
465
+ <figure class="f-pre-code" role="doc-example">
466
+ <figcaption><span class="f-pre-code-label">Code<span class="f-pre-code-label-joint">.</span></span> <strong class="f-pre-code-filename">filename.js</strong></figcaption>
467
+ <pre><code class="language-js">console.log('Nyaan!');
468
+ </code></pre>
469
+ </figure>
470
+ <p>A paragraph.</p>
471
+
472
+ [Markdown]
473
+ A paragraph.
474
+
475
+ Code. **filename.js**
476
+
477
+ ```js
478
+ console.log('Nyaan!');
479
+ ```
480
+
481
+ A paragraph.
482
+
483
+ [HTML]
484
+ <p>A paragraph.</p>
485
+ <figure class="f-pre-code" role="doc-example">
486
+ <figcaption><span class="f-pre-code-label">Code<span class="f-pre-code-label-joint">.</span></span> <strong class="f-pre-code-filename">filename.js</strong></figcaption>
487
+ <pre><code class="language-js">console.log('Nyaan!');
488
+ </code></pre>
489
+ </figure>
490
+ <p>A paragraph.</p>
491
+
492
+ [Markdown]
493
+ ![Figure](cat.jpg)
494
+
495
+ A paragraph.
496
+
497
+ ![Figure](cat.jpg)
498
+
499
+ A paragraph.
500
+
501
+ [HTML]
502
+ <figure class="f-img">
503
+ <img src="cat.jpg" alt="Figure">
504
+ </figure>
505
+ <p>A paragraph.</p>
506
+ <figure class="f-img">
507
+ <img src="cat.jpg" alt="Figure">
508
+ </figure>
509
+ <p>A paragraph.</p>
package/test/test.js CHANGED
@@ -5,7 +5,13 @@ const md = require('markdown-it')({
5
5
  });
6
6
  const mdFigureWithPCaption = require('../index.js');
7
7
 
8
- md.use(mdFigureWithPCaption);
8
+
9
+ md.use(mdFigureWithPCaption, {
10
+ dquoteFilename: true,
11
+ strongFilename: true,
12
+ oneImageWithoutCaption: true,
13
+ });
14
+
9
15
 
10
16
  const example = __dirname + '/examples.txt';
11
17
  const mdPath = __dirname + '/examples.md';