npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

markdown-pdf-marked

v1.0.8

Published

Markdown to PDF converter

Downloads

11

Readme

#改版与原版的不同

修改markdown-pdf 源码

1

  var fs = require('fs')
  var path = require('path')
  var childProcess = require('child_process')
  var through = require('through2')
  var extend = require('extend')
  var Remarkable = require('remarkable')
  var marked = require('marked.js')
  var hljs = require('highlight.js')
  var tmp = require('tmp')
  var duplexer = require('duplexer')
  var streamft = require('stream-from-to')

在头部引入里面,增加了marked渲染器的引入

  var marked = require('marked.js')

2

紧接在后添加 marked的配置

var renderer = new marked.Renderer();
// marked里重新定义了escape 和unescape 函数,重写renderer.code 方法的时候会用到,所以在这里再定义一次
function escape(html, encode) {
  return html
    .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;')
    .replace(/'/g, '&#39;');
}

function unescape(html) {
  // explicitly match decimal, hex, and named HTML entities 
  return html.replace(/&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/g, function(_, n) {
    n = n.toLowerCase();
    if (n === 'colon') return ':';
    if (n.charAt(0) === '#') {
      return n.charAt(1) === 'x'
        ? String.fromCharCode(parseInt(n.substring(2), 16))
        : String.fromCharCode(+n.substring(1));
    }
    return '';
  });
}

renderer.code = function(code, lang, escaped) {
    if (this.options.highlight) {
        var out = this.options.highlight(code, lang);
        if (out != null && out !== code) {
            escaped = true;
            code = out;
        }
    }

    if (!lang) {
        return '<pre><code>' +
            (escaped ? code : escape(code, true)) +
            '\n</code></pre>';
    }

    return '<pre><code class="' +
        this.options.langPrefix +
        escape(lang, true) +
        ' hljs">' +
        (escaped ? code : escape(code, true)) +
        '\n</code></pre>\n';
};
renderer.link = function(href, title, text) {
    if (this.options.sanitize) {
        try {
            var prot = decodeURIComponent(unescape(href))
                .replace(/[^\w:]/g, '')
                .toLowerCase();
        } catch (e) {
            return '';
        }
        if (prot.indexOf('javascript:') === 0 || prot.indexOf('vbscript:') === 0 || prot.indexOf('data:') === 0) {
            return '';
        }
    }
    var solveText = text.toString().split('|');
    var strTarget = '';
    if (solveText[1]) {
        strTarget = 'target="_' + solveText[1].replace(/\s/g, '') + '"';
    }
    var out = '<a ' + strTarget + ' href="' + href + '"';
    if (title) {
        out += ' title="' + title + '"';
    }
    out += '>' + solveText[0] + '</a>';
    return out;
};
renderer.image = function(href, title, text) {
    var imgAttr = '';
    if (text) {
        var s = text.toString().replace(/\s/, '').split('|');
        if (s[0]) {
            s[0] = s[0].replace(/&#39;/g, '');
            imgAttr += ' alt="' + s[0].replace(/@/, '') + '"';
            console.log(imgAttr);
        }
        if (s[1]) {
            imgAttr += ' style="text-align:' + s[1] + ';';
        }
        if (s[2]) {
            var w = s[2].split('x')[0];
            var h = s[2].split('x')[1];
            w = w == 0 ? 'auto' : (w + 'px');
            h = h == 0 ? 'auto' : h + ('px');
            imgAttr += 'width:' + w + ';height:' + h + ';';
        }
        imgAttr += '" ';
    }
    var out = '<img src="' + href + imgAttr;
    if (title) {
        out += ' title="' + title + '"';
    }
    out += this.options.xhtml ? '/>' : '>';
    return out;
};
marked.setOptions({
    renderer: renderer,
    langPrefix: '',
    gfm: true,
    tables: true,
    breaks: false,
    pedantic: false,
    sanitize: false,
    smartLists: true,
    smartypants: false,
    highlight: function (code, lang) {
      // return require('highlight.js').highlightAuto(code).value;
      // console.log(require('highlight.js').highlight(lang, code).value)
      // console.log(code, lang)
      return require('highlight.js').highlight(lang, code).value;
    }
});

在上面 重写了 renderer.code 、renderer.link 、renderer.image 方法,在renderer.code里面的

  return '<pre><code class="' +
        this.options.langPrefix +
        escape(lang, true) +
        ' hljs">' +
        (escaped ? code : escape(code, true)) +
        '\n</code></pre>\n';

里面添加了 hljs类名

因为使用

  require('highlight.js').highlight(lang, code).value; 

进行渲染,语言标签不会加上hljs类名,导致最后样式偏差

3

  function flush (cb) {
      var self = this

      var mdParser = new Remarkable(opts.remarkable.preset, extend({
        highlight: function (str, lang) {
          if (lang && hljs.getLanguage(lang)) {
            try {
              return hljs.highlight(lang, str).value
            } catch (err) {}
          }

          try {
            return hljs.highlightAuto(str).value
          } catch (err) {}

          return ''
        }
      }, opts.remarkable))

      opts.remarkable.plugins.forEach(function (plugin) {
        if (plugin && typeof plugin === 'function') {
          mdParser.use(plugin)
        }
      })

      opts.remarkable.syntax.forEach(function (rule) {
        try {
          mdParser.core.ruler.enable([rule])
        } catch (err) {}
        try {
          mdParser.block.ruler.enable([rule])
        } catch (err) {}
        try {
          mdParser.inline.ruler.enable([rule])
        } catch (err) {}
      })

      // self.push(mdParser.render(md))
      console.log(marked(md))
      self.push(marked(md))
      self.push(null)
    }

在上面的flush函数里面 注释了

  self.push(mdParser.render(md))

添加了

  self.push(marked(md))

查看原版ReadMe.md

originReadMe.md