Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Modify first h1/p just like npm does
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Oct 28, 2016
1 parent b3fd5bc commit c67d598
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
21 changes: 21 additions & 0 deletions bin/our-marky-markdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node
// Extend marky-markdown.js to support the package argument:
// https://www.npmjs.com/package/marky-markdown#npm-packages

var fs = require('fs')
var path = require('path')
var marky = require('marky-markdown')

if (process.argv.length < 3) {
console.log('Usage:\n\nour-marky-markdown some.md pkg > some.html')
process.exit()
}

var filePath = path.resolve(process.cwd(), process.argv[2])

fs.readFile(filePath, function (err, data) {
if (err) throw err;
var package = process.argv[3] ? JSON.parse(process.argv[3]) : null;
var $ = marky(data.toString(), {package: package})
process.stdout.write($.html())
})
19 changes: 16 additions & 3 deletions gratipay/utils/markdown.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from subprocess import Popen, PIPE

from markupsafe import Markup
import json
import misaka as m # http://misaka.61924.nl/
from markupsafe import Markup


def render(markdown):
Expand All @@ -12,10 +13,22 @@ def render(markdown):
))


def marky(markdown):
def marky(markdown, package=None):
"""Process markdown the same way npm does.
Package should be a dict representing the package. If it includes `name`
and `description` then the first h1 and paragraph will have a
'package-{name,description}-redundant' class added to them if they're
similar enough. If it includes `repository.url` then links will be changed
somehow. For details consult the docs and code:
https://github.com/npm/marky-markdown
"""
if type(markdown) is unicode:
markdown = markdown.encode('utf8')
marky = Popen(("marky-markdown", "/dev/stdin"), stdin=PIPE, stdout=PIPE)
cmd = ("bin/our-marky-markdown.js", "/dev/stdin")
if package:
cmd += (json.dumps(package),)
marky = Popen(cmd, stdin=PIPE, stdout=PIPE)
return Markup(marky.communicate(markdown)[0])
9 changes: 9 additions & 0 deletions tests/py/test_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ def test_marky_works(self):
actual = HTMLParser().unescape(markdown.marky(md)).strip()
expected = '<p><strong>Hello World!</strong></p>'
assert actual == expected

def test_marky_handles_npm_package(self):
md = "# Greetings, program!\nGreetings. Program."
pkg = {'name': 'greetings-program', 'description': 'Greetings, program.'}
actual = HTMLParser().unescape(markdown.marky(md, pkg)).strip()
expected = '''\
<h1 id="user-content-greetings-program" class="deep-link package-name-redundant package-description-redundant"><a href="#greetings-program">Greetings, program!</a></h1>
<p class="package-description-redundant">Greetings. Program.</p>'''
assert actual == expected

0 comments on commit c67d598

Please sign in to comment.