Sha256: 43aeab598d2e5c95290bd306ea8e30ce6036cd9e407fa247a7dce0e62b2a43d8

Contents?: true

Size: 1.7 KB

Versions: 23

Compression:

Stored size: 1.7 KB

Contents

'use strict'

var entityPrefixLength = require('./entity-prefix-length')

module.exports = copy

var ampersand = '&'

var punctuationExppresion = /[-!"#$%&'()*+,./:;<=>?@[\\\]^`{|}~_]/

// For shortcut and collapsed reference links, the contents is also an
// identifier, so we need to restore the original encoding and escaping
// that were present in the source string.
//
// This function takes the unescaped & unencoded value from shortcut’s
// child nodes and the identifier and encodes the former according to
// the latter.
function copy(value, identifier) {
  var length = value.length
  var count = identifier.length
  var result = []
  var position = 0
  var index = 0
  var start

  while (index < length) {
    // Take next non-punctuation characters from `value`.
    start = index

    while (index < length && !punctuationExppresion.test(value.charAt(index))) {
      index += 1
    }

    result.push(value.slice(start, index))

    // Advance `position` to the next punctuation character.
    while (
      position < count &&
      !punctuationExppresion.test(identifier.charAt(position))
    ) {
      position += 1
    }

    // Take next punctuation characters from `identifier`.
    start = position

    while (
      position < count &&
      punctuationExppresion.test(identifier.charAt(position))
    ) {
      if (identifier.charAt(position) === ampersand) {
        position += entityPrefixLength(identifier.slice(position))
      }

      position += 1
    }

    result.push(identifier.slice(start, position))

    // Advance `index` to the next non-punctuation character.
    while (index < length && punctuationExppresion.test(value.charAt(index))) {
      index += 1
    }
  }

  return result.join('')
}

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
trusty-cms-5.0.7 node_modules/remark-stringify/lib/util/copy-identifier-encoding.js
trusty-cms-5.0.6 node_modules/remark-stringify/lib/util/copy-identifier-encoding.js
trusty-cms-5.0.5 node_modules/remark-stringify/lib/util/copy-identifier-encoding.js
trusty-cms-5.0.4 node_modules/remark-stringify/lib/util/copy-identifier-encoding.js
trusty-cms-5.0.3 node_modules/remark-stringify/lib/util/copy-identifier-encoding.js
trusty-cms-5.0.2 node_modules/remark-stringify/lib/util/copy-identifier-encoding.js
trusty-cms-5.0.1 node_modules/remark-stringify/lib/util/copy-identifier-encoding.js
trusty-cms-4.3.5 node_modules/remark-stringify/lib/util/copy-identifier-encoding.js
trusty-cms-5.0.0 node_modules/remark-stringify/lib/util/copy-identifier-encoding.js
trusty-cms-4.3.4 node_modules/remark-stringify/lib/util/copy-identifier-encoding.js
trusty-cms-4.3.3 node_modules/remark-stringify/lib/util/copy-identifier-encoding.js
trusty-cms-4.3.2 node_modules/remark-stringify/lib/util/copy-identifier-encoding.js
trusty-cms-4.3.1 node_modules/remark-stringify/lib/util/copy-identifier-encoding.js
trusty-cms-4.3 node_modules/remark-stringify/lib/util/copy-identifier-encoding.js
trusty-cms-4.2.3 node_modules/remark-stringify/lib/util/copy-identifier-encoding.js
trusty-cms-4.2.2 node_modules/remark-stringify/lib/util/copy-identifier-encoding.js
trusty-cms-4.2.1 node_modules/remark-stringify/lib/util/copy-identifier-encoding.js
trusty-cms-4.2 node_modules/remark-stringify/lib/util/copy-identifier-encoding.js
trusty-cms-4.1.9 node_modules/remark-stringify/lib/util/copy-identifier-encoding.js
trusty-cms-4.1.8 node_modules/remark-stringify/lib/util/copy-identifier-encoding.js