@peaceroad/markdown-it-footnote-here 0.3.1 → 0.3.3

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.
Files changed (2) hide show
  1. package/index.js +29 -11
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -194,14 +194,25 @@ const footnote_plugin = (md, option) =>{
194
194
 
195
195
  const isEndnote = isEndnoteLabel(label, opt)
196
196
  const fn = ensureNotesEnv(state.env, isEndnote ? 'endnotes' : 'footnotes')
197
- const id = fn.length++
198
- fn.refs[':' + label] = id
197
+ const refKey = ':' + label
198
+ const existingId = fn.refs[refKey]
199
+ const isDuplicate = isEndnote && existingId !== undefined
200
+ const id = isDuplicate ? existingId : fn.length++
201
+ if (!isDuplicate) {
202
+ fn.refs[refKey] = id
203
+ }
199
204
 
200
- const token = new state.Token('footnote_open', '', 1)
201
- token.meta = { id, isEndnote }
202
- token.level = state.level++
203
- state.tokens.push(token)
204
- fn.positions.push(state.tokens.length - 1)
205
+ let tokenStart = 0
206
+ let openToken = null
207
+ if (!isDuplicate) {
208
+ openToken = new state.Token('footnote_open', '', 1)
209
+ openToken.meta = { id, isEndnote }
210
+ openToken.level = state.level++
211
+ state.tokens.push(openToken)
212
+ fn.positions.push(state.tokens.length - 1)
213
+ } else {
214
+ tokenStart = state.tokens.length
215
+ }
205
216
 
206
217
  const oldBMark = bMarks[startLine]
207
218
  const oldTShift = tShift[startLine]
@@ -239,6 +250,9 @@ const footnote_plugin = (md, option) =>{
239
250
  }
240
251
 
241
252
  state.md.block.tokenize(state, startLine, endLine, true)
253
+ if (openToken) {
254
+ openToken.map = [startLine, state.line]
255
+ }
242
256
 
243
257
  state.parentType = oldParentType
244
258
  state.blkIndent -= 4
@@ -246,10 +260,14 @@ const footnote_plugin = (md, option) =>{
246
260
  state.sCount[startLine] = oldSCount
247
261
  state.bMarks[startLine] = oldBMark
248
262
 
249
- const closeToken = new state.Token('footnote_close', '', -1)
250
- closeToken.level = --state.level
251
- closeToken.meta = { isEndnote }
252
- state.tokens.push(closeToken)
263
+ if (!isDuplicate) {
264
+ const closeToken = new state.Token('footnote_close', '', -1)
265
+ closeToken.level = --state.level
266
+ closeToken.meta = { isEndnote }
267
+ state.tokens.push(closeToken)
268
+ } else {
269
+ state.tokens.length = tokenStart
270
+ }
253
271
 
254
272
  return true
255
273
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peaceroad/markdown-it-footnote-here",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "description": "A markdown-it plugin. This generate aside[role|doc-footnote] element just below the footnote reference paragraph.",
5
5
  "main": "index.js",
6
6
  "type":"module",