Sha256: 4cf9d251fa21fc40771bed26eb744c918e4a3670bab15ca08a3a0428fc2a3458

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

module.exports = link
link.peek = linkPeek

var checkQuote = require('../util/check-quote')
var formatLinkAsAutolink = require('../util/format-link-as-autolink')
var phrasing = require('../util/container-phrasing')
var safe = require('../util/safe')

function link(node, _, context) {
  var quote = checkQuote(context)
  var suffix = quote === '"' ? 'Quote' : 'Apostrophe'
  var exit
  var subexit
  var value
  var stack

  if (formatLinkAsAutolink(node, context)) {
    // Hide the fact that we’re in phrasing, because escapes don’t work.
    stack = context.stack
    context.stack = []
    exit = context.enter('autolink')
    value = '<' + phrasing(node, context, {before: '<', after: '>'}) + '>'
    exit()
    context.stack = stack
    return value
  }

  exit = context.enter('link')
  subexit = context.enter('label')
  value = '[' + phrasing(node, context, {before: '[', after: ']'}) + ']('
  subexit()

  if (
    // If there’s no url but there is a title…
    (!node.url && node.title) ||
    // Or if there’s markdown whitespace or an eol, enclose.
    /[ \t\r\n]/.test(node.url)
  ) {
    subexit = context.enter('destinationLiteral')
    value += '<' + safe(context, node.url, {before: '<', after: '>'}) + '>'
  } else {
    // No whitespace, raw is prettier.
    subexit = context.enter('destinationRaw')
    value += safe(context, node.url, {
      before: '(',
      after: node.title ? ' ' : ')'
    })
  }

  subexit()

  if (node.title) {
    subexit = context.enter('title' + suffix)
    value +=
      ' ' +
      quote +
      safe(context, node.title, {before: quote, after: quote}) +
      quote
    subexit()
  }

  value += ')'

  exit()
  return value
}

function linkPeek(node, _, context) {
  return formatLinkAsAutolink(node, context) ? '<' : '['
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trusty-cms-6.3.1 node_modules/mdast-util-to-markdown/lib/handle/link.js