Sha256: 632c87d5241455c4476d69bc5f715dc73d5b948fab33dd10ca201280d4fdc584

Contents?: true

Size: 1.22 KB

Versions: 23

Compression:

Stored size: 1.22 KB

Contents

'use strict'

var streak = require('longest-streak')
var repeat = require('repeat-string')

module.exports = inlineCode

var graveAccentChar = '`'
var lineFeed = 10 //  '\n'
var space = 32 // ' '
var graveAccent = 96 //  '`'

// Stringify inline code.
//
// Knows about internal ticks (`\``), and ensures one more tick is used to
// enclose the inline code:
//
// ````markdown
// ```foo ``bar`` baz```
// ````
//
// Even knows about inital and final ticks:
//
// ``markdown
// `` `foo ``
// `` foo` ``
// ```
function inlineCode(node) {
  var value = node.value
  var ticks = repeat(graveAccentChar, streak(value, graveAccentChar) + 1)
  var start = ticks
  var end = ticks
  var head = value.charCodeAt(0)
  var tail = value.charCodeAt(value.length - 1)
  var wrap = false
  var index
  var length

  if (head === graveAccent || tail === graveAccent) {
    wrap = true
  } else if (value.length > 2 && ws(head) && ws(tail)) {
    index = 1
    length = value.length - 1

    while (++index < length) {
      if (!ws(value.charCodeAt(index))) {
        wrap = true
        break
      }
    }
  }

  if (wrap) {
    start += ' '
    end = ' ' + end
  }

  return start + value + end
}

function ws(code) {
  return code === lineFeed || code === space
}

Version data entries

23 entries across 23 versions & 1 rubygems

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