@peaceroad/markdown-it-figure-with-p-caption 0.11.0 → 0.13.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,11 +2,12 @@
2
2
 
3
3
  This is a markdown-it plugin.
4
4
 
5
- For a paragraph with one image/images only, a table or code block or a blockquote or a iframe, and by writing a caption paragraph immediately before or after, they are converted into the figure element with the figcaption element.
5
+ For paragraphs containing only images, tables, code blocks, blockquotes, or iframes, this plugin converts them into figure elements with figcaption elements when a caption paragraph is written immediately before or after.
6
6
 
7
- 1. Check that the element: one image only paragraph, table, code(samp) block, blockquote, and video, iframe.
8
- 2. Check if this element has a caption paragraph immediately before or after it
9
- 3. If there is the caption paragraph, convert them to figure and figcaption element.
7
+ The conversion process:
8
+ 1. Detect supported elements: image paragraphs, tables, code/samp blocks, blockquotes, videos, and iframes
9
+ 2. Check for caption paragraphs immediately before or after the element
10
+ 3. Convert both elements into a figure with figcaption structure
10
11
 
11
12
  The figcaption behavior of this plugin depends on [p7d-markdown-it-p-captions](https://www.npmjs.com/package/p7d-markdown-it-p-captions).
12
13
 
@@ -36,6 +37,11 @@ Also, It is recommended to set the width and height attributes of the images at
36
37
 
37
38
  It could be applied to table, codeblock(pre > code, pre > samp), video as well.
38
39
 
40
+ These elements are also supported within the following structure. [0.13.0+]
41
+
42
+ - Blockquote
43
+ - loose list (with blank lines between items), not tight list (no blank lines)
44
+ - Description list block (`<dl>` markup, markdown-it-deflist)
39
45
 
40
46
  ## Example
41
47
 
@@ -1,49 +1,36 @@
1
1
  import { markReg } from 'p7d-markdown-it-p-captions'
2
2
 
3
- const imgReg = /^( *!\[)(.*?)\]\( *?((.*?)(?: +?\"(.*?)\")?) *?\)( *?\{.*?\})? *$/
3
+ const imgReg = /^( *!\[)(.*?)\]\( *?((.*?)(?: +?"(.*?)")?) *?\)( *?\{.*?\})? *$/
4
4
 
5
5
  const imgAttrToPCaption = (state, startLine, opt) => {
6
+ const imgMarkReg = markReg['img']
6
7
  let pos = state.bMarks[startLine] + state.tShift[startLine]
7
8
  let max = state.eMarks[startLine]
8
- //console.log('init inline: ' + state.src.slice(pos, max))
9
9
  let inline = state.src.slice(pos, max)
10
10
  const img = inline.match(imgReg)
11
11
  if (!img) return
12
12
 
13
- let alt = img[2] === undefined ? '' : img[2]
14
- let title = img[5] === undefined ? '' : img[5]
15
- //console.log('alt: ' + alt + ', title: ' + title)
16
-
17
- let caption = ''
18
- if (opt.imgAltCaption) caption = alt
19
- if (opt.imgTitleCaption) caption = title
20
-
21
- const hasMarkLabel = caption.match(markReg['img'])
13
+ let alt = img[2] ?? ''
14
+ let title = img[5] ?? ''
15
+ const caption = opt.imgTitleCaption ? title : (opt.imgAltCaption ? alt : '')
16
+ const altCap = typeof opt.imgAltCaption === 'string' ? opt.imgAltCaption : ''
17
+ const titleCap = typeof opt.imgTitleCaption === 'string' ? opt.imgTitleCaption : ''
22
18
 
19
+ const hasMarkLabel = caption.match(imgMarkReg)
23
20
  let modCaption = ''
24
21
  if (hasMarkLabel) {
25
22
  modCaption = caption
26
23
  } else {
27
- if (typeof opt.imgAltCaption === 'string' || typeof opt.imgTitleCaption === 'string') {
28
- if (/[a-zA-Z]/.test(opt.imgAltCaption)) {
29
- if (caption === '') {
30
- modCaption = (opt.imgAltCaption ? opt.imgAltCaption : opt.imgTitleCaption) + '.'
31
- } else {
32
- modCaption = (opt.imgAltCaption ? opt.imgAltCaption : opt.imgTitleCaption) + '. ' + caption
33
- }
34
- } else {
35
- if (caption === '') {
36
- modCaption = (opt.imgAltCaption ? opt.imgAltCaption : opt.imgTitleCaption) + ' '
37
- } else {
38
- modCaption = (opt.imgAltCaption ? opt.imgAltCaption : opt.imgTitleCaption) + ' ' + caption
39
- }
40
- }
24
+ const prefix = altCap || titleCap || ''
25
+ if (prefix && /[a-zA-Z]/.test(prefix)) {
26
+ modCaption = caption === '' ? prefix + '.' : prefix + '. ' + caption
41
27
  } else {
42
- modCaption = 'Figure.'
43
- if (caption !== '') modCaption += ' ' + caption
28
+ modCaption = caption === '' ? prefix + ' ' : prefix + ' ' + caption
29
+ }
30
+ if (!prefix) {
31
+ modCaption = 'Figure.' + (caption !== '' ? ' ' + caption : '')
44
32
  }
45
33
  }
46
- //console.log('modCaption: ' + modCaption)
47
34
  let token = state.push('paragraph_open', 'p', 1)
48
35
  token.map = [startLine, startLine + 1]
49
36
  token = state.push('inline', '', 0)
@@ -58,46 +45,37 @@ const imgAttrToPCaption = (state, startLine, opt) => {
58
45
 
59
46
  const setAltToLabel = (state, n) => {
60
47
  if (n < 2) return false
61
- if (state.tokens[n+1].children[0].type !== 'image' || !state.tokens[n-2].children) return false
48
+ const imageToken = state.tokens[n+1].children[0]
49
+ if (imageToken.type !== 'image' || !state.tokens[n-2].children) return false
50
+ const prevTokenChild = state.tokens[n-2].children[0]
62
51
  if (state.tokens[n-2].children) {
63
- state.tokens[n+1].content = state.tokens[n+1].content.replace(/^!\[.*?\]/, '![' + state.tokens[n-2].children[0].content + ']')
64
- if (!state.tokens[n+1].children[0].children[0]) {
52
+ state.tokens[n+1].content = state.tokens[n+1].content.replace(/^!\[.*?\]/, '![' + prevTokenChild.content + ']')
53
+ if (!imageToken.children[0]) {
65
54
  const textToken = new state.Token('text', '', 0)
66
- state.tokens[n+1].children[0].children.push(textToken)
55
+ imageToken.children.push(textToken)
67
56
  }
68
- // Set figure label:
69
- //state.tokens[n+1].children[0].children[0].content = state.tokens[n-2].children[2].content
70
- // Set img alt to empty value:
71
- state.tokens[n+1].children[0].children[0].content = ''
57
+ imageToken.children[0].content = ''
72
58
  }
73
- // Set figure label:
74
- //state.tokens[n+1].children[0].content = state.tokens[n-2].children[2].content
75
- // Set img alt to empty value:
76
- state.tokens[n+1].children[0].content = ''
77
- //console.log(state.tokens[n+1].children[0])
59
+ imageToken.content = ''
78
60
  return true
79
61
  }
80
62
 
81
63
  const setTitleToLabel = (state, n) => {
82
64
  if (n < 2) return false
83
- if (state.tokens[n+1].children[0].type !== 'image') return false
65
+ const imageToken = state.tokens[n+1].children[0]
66
+ if (imageToken.type !== 'image') return false
84
67
  if (!state.tokens[n-2].children[0]) return false
85
- state.tokens[n+1].children[0].attrSet('alt', state.tokens[n+1].children[0].content)
86
- if (!state.tokens[n+1].children[0].children[0]) {
68
+ imageToken.attrSet('alt', imageToken.content)
69
+ if (!imageToken.children[0]) {
87
70
  const textToken = new state.Token('text', '', 0)
88
- state.tokens[n+1].children[0].children.push(textToken)
71
+ imageToken.children.push(textToken)
89
72
  }
90
- let i = 0
91
- while (0 < state.tokens[n+1].children[0].attrs.length) {
92
- if (state.tokens[n+1].children[0].attrs[i][0] === 'title') {
93
- state.tokens[n+1].children[0].attrs.splice(i, i + 1)
73
+ for (let i = 0; i < imageToken.attrs.length; i++) {
74
+ if (imageToken.attrs[i][0] === 'title') {
75
+ imageToken.attrs.splice(i, 1)
94
76
  break
95
- } else {
96
- state.tokens[n+1].children[0].attrJoin('title', '')
97
77
  }
98
- i++
99
78
  }
100
- //console.log(state.tokens[n+1].children[0])
101
79
  return true
102
80
  }
103
81