@peaceroad/markdown-it-strong-ja 0.5.5 → 0.5.6

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 +64 -32
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -293,16 +293,7 @@ const findInlineLinkRange = (pos, ranges, kind) => {
293
293
  }
294
294
 
295
295
  const copyInlineTokenFields = (dest, src) => {
296
- if (src.attrs) dest.attrs = src.attrs
297
- if (src.map) dest.map = src.map
298
- dest.level = src.level
299
- if (src.children) dest.children = src.children
300
- dest.content = src.content
301
- dest.markup = src.markup
302
- if (src.info) dest.info = src.info
303
- if (src.meta) dest.meta = src.meta
304
- dest.block = src.block
305
- dest.hidden = src.hidden
296
+ Object.assign(dest, src)
306
297
  }
307
298
 
308
299
  const registerPostProcessTarget = (state) => {
@@ -352,13 +343,49 @@ function isPlainTextContent(content) {
352
343
  if (code === CHAR_BACKSLASH || code === CHAR_NEWLINE || code === CHAR_TAB) {
353
344
  return false
354
345
  }
355
- if (code < 128 && isAsciiPunctuationCode(code)) return false
346
+ if (code === CHAR_BACKTICK || code === CHAR_DOLLAR || code === CHAR_LT || code === CHAR_GT) {
347
+ return false
348
+ }
349
+ if (code === CHAR_OPEN_BRACKET || code === CHAR_CLOSE_BRACKET || code === CHAR_OPEN_PAREN || code === CHAR_CLOSE_PAREN) {
350
+ return false
351
+ }
352
+ if (code === 0x5E || code === 0x7E) {
353
+ return false
354
+ }
356
355
  }
357
356
  return true
358
357
  }
359
358
 
359
+ // Cache newline positions for lightweight map generation
360
+ const getLineOffsets = (state) => {
361
+ if (state.__strongJaLineOffsets) return state.__strongJaLineOffsets
362
+ const offsets = []
363
+ const src = state.src || ''
364
+ for (let i = 0; i < src.length; i++) {
365
+ if (src.charCodeAt(i) === CHAR_NEWLINE) offsets.push(i)
366
+ }
367
+ state.__strongJaLineOffsets = offsets
368
+ return offsets
369
+ }
370
+
371
+ const createLineMapper = (state) => {
372
+ const offsets = getLineOffsets(state)
373
+ let idx = 0
374
+ const maxIdx = offsets.length
375
+ return (startPos, endPos) => {
376
+ const start = startPos === undefined || startPos === null ? 0 : startPos
377
+ const end = endPos === undefined || endPos === null ? start : endPos
378
+ while (idx < maxIdx && offsets[idx] < start) idx++
379
+ const startLine = idx
380
+ let endIdx = idx
381
+ while (endIdx < maxIdx && offsets[endIdx] < end) endIdx++
382
+ return [startLine, endIdx]
383
+ }
384
+ }
385
+
360
386
  const setToken = (state, inlines, opt, attrsEnabled) => {
361
387
  const src = state.src
388
+ const mapFromPos = createLineMapper(state)
362
389
  let i = 0
363
390
  let attrsIsText = false
364
391
  let attrsIsTextTag = ''
@@ -378,6 +405,7 @@ const setToken = (state, inlines, opt, attrsEnabled) => {
378
405
  if (isOpen) {
379
406
  const startToken = state.push(type, tag, 1)
380
407
  startToken.markup = tag === 'strong' ? '**' : '*'
408
+ startToken.map = mapFromPos(inlines[i].s, inlines[i].e)
381
409
  attrsIsText = true
382
410
  attrsIsTextTag = tag
383
411
  }
@@ -391,6 +419,7 @@ const setToken = (state, inlines, opt, attrsEnabled) => {
391
419
  if (isAllAsterisks(content)) {
392
420
  const asteriskToken = state.push(type, '', 0)
393
421
  asteriskToken.content = content
422
+ asteriskToken.map = mapFromPos(inlines[i].s, inlines[i].e)
394
423
  i++
395
424
  continue
396
425
  }
@@ -422,6 +451,7 @@ const setToken = (state, inlines, opt, attrsEnabled) => {
422
451
  }
423
452
  attrsIsText = false
424
453
  attrsIsTextTag = ''
454
+ attrsToken.map = mapFromPos(inlines[i].s, inlines[i].e)
425
455
  i++
426
456
  continue
427
457
  }
@@ -429,27 +459,31 @@ const setToken = (state, inlines, opt, attrsEnabled) => {
429
459
  if (isPlainTextContent(content)) {
430
460
  const textToken = state.push(type, '', 0)
431
461
  textToken.content = content
462
+ textToken.map = mapFromPos(inlines[i].s, inlines[i].e)
432
463
  i++
433
464
  continue
434
465
  }
435
466
 
436
467
  const childTokens = state.md.parseInline(content, state.env)
437
- if (childTokens[0] && childTokens[0].children) {
438
- let j = 0
439
- while (j < childTokens[0].children.length) {
440
- const t = childTokens[0].children[j]
441
- if (t.type === 'softbreak' && !opt.mdBreaks) {
442
- t.type = 'text'
443
- t.tag = ''
444
- t.content = '\n'
445
- }
446
- if (!attrsEnabled && t.tag === 'br') {
447
- t.tag = ''
448
- t.content = '\n'
468
+ for (let k = 0; k < childTokens.length; k++) {
469
+ const parentToken = childTokens[k]
470
+ if (parentToken && parentToken.children) {
471
+ let j = 0
472
+ while (j < parentToken.children.length) {
473
+ const t = parentToken.children[j]
474
+ if (t.type === 'softbreak' && !opt.mdBreaks) {
475
+ t.type = 'text'
476
+ t.tag = ''
477
+ t.content = '\n'
478
+ }
479
+ if (!attrsEnabled && t.tag === 'br') {
480
+ t.tag = ''
481
+ t.content = '\n'
482
+ }
483
+ const token = state.push(t.type, t.tag, t.nesting)
484
+ copyInlineTokenFields(token, t)
485
+ j++
449
486
  }
450
- const token = state.push(t.type, t.tag, t.nesting)
451
- copyInlineTokenFields(token, t)
452
- j++
453
487
  }
454
488
  }
455
489
  }
@@ -457,6 +491,7 @@ const setToken = (state, inlines, opt, attrsEnabled) => {
457
491
  if (isClose) {
458
492
  const closeToken = state.push(type, tag, -1)
459
493
  closeToken.markup = tag === 'strong' ? '**' : '*'
494
+ closeToken.map = mapFromPos(inlines[i].s, inlines[i].e)
460
495
  attrsIsText = false
461
496
  attrsIsTextTag = ''
462
497
  }
@@ -1375,13 +1410,10 @@ const adjustTokenLevels = (tokens, startIdx, endIdx, delta) => {
1375
1410
 
1376
1411
  const cloneTextToken = (source, content) => {
1377
1412
  const newToken = new Token('text', '', 0)
1413
+ Object.assign(newToken, source)
1378
1414
  newToken.content = content
1379
- newToken.level = source.level
1380
- newToken.markup = source.markup
1381
- newToken.info = source.info
1382
- newToken.meta = source.meta ? {...source.meta} : null
1383
- newToken.block = source.block
1384
- newToken.hidden = source.hidden
1415
+ if (source.meta) newToken.meta = { ...source.meta }
1416
+ if (source.map) newToken.map = source.map
1385
1417
  return newToken
1386
1418
  }
1387
1419
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@peaceroad/markdown-it-strong-ja",
3
3
  "description": "This is a plugin for markdown-it. It is an alternative to the standard `**` (strong) and `*` (em) processing. It also processes strings that cannot be converted by the standard.",
4
- "version": "0.5.5",
4
+ "version": "0.5.6",
5
5
  "main": "index.js",
6
6
  "type": "module",
7
7
  "files": [