Skip to content
This repository has been archived by the owner on Aug 28, 2017. It is now read-only.

Commit

Permalink
Merge pull request #2 from nteract/TF2.0
Browse files Browse the repository at this point in the history
TF 2.0
  • Loading branch information
willwhitney committed Aug 1, 2015
2 parents 700b5e6 + b54f93a commit 3f7c993
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 33 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ npm install transformime-commonmark
Usage with transformime:

```
> MarkdownTransformer = require('transformime-commonmark');
> mdt = new MarkdownTransformer();
> markdownTransform = require('transformime-commonmark');
> el = mdt.transform("```python\nimport this\n```\n", document);
> el = mdt.transform("text/markdown", "```python\nimport this\n```\n", document);
> el.innerHTML
'<pre><code class="language-python">import this\n</code></pre>\n'
```
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"name": "transformime-commonmark",
"version": "0.0.1",
"description": "Transforms Markdown to HTML Elements, adhering to the transformime interface",
"main": "lib/commonmark-transformer.js",
"main": "lib/commonmark.transform.js",
"scripts": {
"prebuild": "mkdirp lib && mkdirp dist && npm run clean",
"test": "mocha --compilers js:babel/register",
"build": "npm run build:es5 && npm run build:browser",
"build:es5": "babel src --out-dir lib/ --source-maps",
"build:browser": "browserify lib/*.js --outfile dist/transformime.js",
"build:browser": "browserify lib/*.js --outfile dist/tf-commonmark.js",
"prepublish": "npm run build",
"watch": "parallelshell 'npm run watch:test -s' 'npm run watch:build -s'",
"watch:build": "nodemon -q -w src/ --ext '.' --exec 'npm run build'",
Expand Down
28 changes: 0 additions & 28 deletions src/commonmark-transformer.js

This file was deleted.

29 changes: 29 additions & 0 deletions src/commonmark.transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use strict";

var commonmark = require("commonmark");

var markdownTransform = function(){
// Stick reader and writer in a closure so they only get created once.

let reader = new commonmark.Parser();
let writer = new commonmark.HtmlRenderer({
safe: true
});

return function(mimetype, data, document) {
var div = document.createElement("div");

var parsed = reader.parse(data);

// TODO: Any other transformations on the parsed object
// See https://github.com/jgm/commonmark.js#usage

div.innerHTML = writer.render(parsed);

return div;
};
}();

markdownTransform.mimetype = 'text/markdown';

export default markdownTransform;
62 changes: 62 additions & 0 deletions test/commonmark.transform.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {assert} from 'chai';

import {jsdom} from 'jsdom';

var markdownTransform = require('../src/commonmark.transform');

describe('text transformer', function() {
beforeEach(function() {
this.document = jsdom();
});

it('should have the text/markdown mimetype', function() {
assert.equal(markdownTransform.mimetype, "text/markdown");
});
describe('#transform', function() {
it('should create a div with nice markup', function() {
let text = `# Ratification of Markdown Association
We are present here because we believe in unification
across all markdowns, whether common, flavored like
GitHub, or used to help an undergrad do their homework
via StackOverflow. It is of *great importance* that we
unify for the [sake of the sciences](https://jupyter.org),
the arts, and be able to express in rich text our thoughts,
feelings, and desires.
## Speaking for some
We need markdown to work
* On the web
* On your hard drive
* Hanging upside down from a bridge
* In letters to your lovers`;
text = text + "\n\n```bash\nnpm install transformime-commonmark\n```\n";

let el = markdownTransform('text/markdown', text, this.document);

assert.equal(el.innerHTML, `<h1>Ratification of Markdown Association</h1>
<p>We are present here because we believe in unification
across all markdowns, whether common, flavored like
GitHub, or used to help an undergrad do their homework
via StackOverflow. It is of <em>great importance</em> that we
unify for the <a href="https://jupyter.org">sake of the sciences</a>,
the arts, and be able to express in rich text our thoughts,
feelings, and desires.</p>
<h2>Speaking for some</h2>
<p>We need markdown to work</p>
<ul>
<li>On the web</li>
<li>On your hard drive</li>
<li>Hanging upside down from a bridge</li>
<li>In letters to your lovers</li>
</ul>
<pre><code class="language-bash">npm install transformime-commonmark
</code></pre>
`);
assert.equal(el.localName, 'div');

});
});
});

0 comments on commit 3f7c993

Please sign in to comment.