github.com/titpetric/pendulum@v0.1.180207-1512.0.20180514135826-1f399445df57/front/src/markdown/index.js (about)

     1  var markdown = {
     2    Transform: function (contents, folder) {
     3      contents = this.transformPageBreaks(contents)
     4      contents = this.transformMeta(contents)
     5      contents = this.transformLeanpub(contents)
     6      contents = this.transformHugo(contents)
     7      contents = this.transformImages(contents, folder)
     8      return contents
     9    },
    10    transformPageBreaks: function (contents) {
    11      var replacement = '<hr class="pagebreak"/>'
    12      contents = contents.replace(/^<!--more-->/gi, replacement)
    13      contents = contents.replace(/^{pagebreak}/gi, replacement)
    14      return contents
    15    },
    16    transformHugo: function(contents, folder) {
    17      contents = contents.replace(/{% asset_img ([^ ]+) %}/g, '![]($1)')
    18      contents = contents.replace(/{% asset_img ([^ ]+) "(.+)" %}/g, '![$2]($1)')
    19      contents = contents.replace(/{% asset_img ([^ ]+) '(.+)' %}/g, '![$2]($1)')
    20      return contents
    21    },
    22    transformImages: function (contents, folder) {
    23      contents = contents.replace(/!\[([^\]]*)\]\(([^\)]+)\)/g, function (m, group) {
    24        if (m.match(/\]\(http/)) {
    25          return m
    26        }
    27        return m.replace('](', '](/contents' + folder + '/')
    28      })
    29      return contents
    30    },
    31    transformLeanpub: function (contents) {
    32      contents = contents.replace(/^A>/g, '>')
    33      return contents
    34    },
    35    transformMeta: function (contents) {
    36      // hugo start of meta
    37      if (contents.substring(0,3) === '---') {
    38        contents = contents.substring(3)
    39      }
    40      // next token is end of meta
    41      if (contents.indexOf('---') === -1) {
    42        return contents
    43      }
    44      var parts = contents.split('---', 2)
    45      var heading = parts[0].trim().split("\n")
    46      var isMeta = true
    47      var headingTable = []
    48      headingTable.push('| Name | Value |')
    49      headingTable.push('|------|-------|')
    50      heading.forEach(function (row) {
    51        if (!isMeta) {
    52          return
    53        }
    54        var columns = row.split(':', 2)
    55        if (columns.length < 2) {
    56          isMeta = false
    57          return
    58        }
    59        if (columns[0].trim() === 'image') {
    60          columns[1] = '![](' + columns[1].trim().replace(/^"(.+)"$/,'$1').replace(/^'(.+)'$/,'$1') + ')'
    61        }
    62        headingTable.push('| ' + columns.join(' | ') + ' |')
    63      })
    64      if (!isMeta) {
    65        return contents
    66      }
    67      parts[0] = headingTable.join("\n")
    68      return parts.join("\n---")
    69    }
    70  }
    71  
    72  if (typeof module === "object") {
    73  	module.exports = Object.create(markdown)
    74  }