2016-09-22 16:18:12 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import marked from 'marked';
|
|
|
|
|
|
|
|
// marked only applies the default options on the high
|
|
|
|
// level marked() interface, so we do it here.
|
2016-09-22 16:51:34 +00:00
|
|
|
const marked_options = Object.assign({}, marked.defaults, {
|
2016-09-22 16:18:12 +00:00
|
|
|
gfm: true,
|
|
|
|
tables: true,
|
|
|
|
breaks: true,
|
|
|
|
pedantic: false,
|
|
|
|
sanitize: true,
|
|
|
|
smartLists: true,
|
|
|
|
smartypants: false,
|
2016-09-22 17:17:02 +00:00
|
|
|
xhtml: true, // return self closing tags (ie. <br /> not <br>)
|
2016-09-22 16:51:34 +00:00
|
|
|
});
|
2016-09-22 16:18:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class that wraps marked, adding the ability to see whether
|
|
|
|
* a given message actually uses any markdown syntax or whether
|
|
|
|
* it's plain text.
|
|
|
|
*/
|
|
|
|
export default class Markdown {
|
|
|
|
constructor(input) {
|
|
|
|
const lexer = new marked.Lexer(marked_options);
|
|
|
|
this.tokens = lexer.lex(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
_copyTokens() {
|
2016-09-23 13:15:48 +00:00
|
|
|
// copy tokens (the parser modifies its input arg)
|
2016-09-22 16:18:12 +00:00
|
|
|
const tokens_copy = this.tokens.slice();
|
|
|
|
// it also has a 'links' property, because this is javascript
|
|
|
|
// and why wouldn't you have an array that also has properties?
|
|
|
|
return Object.assign(tokens_copy, this.tokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
isPlainText() {
|
|
|
|
// we determine if the message requires markdown by
|
|
|
|
// running the parser on the tokens with a dummy
|
|
|
|
// rendered and seeing if any of the renderer's
|
|
|
|
// functions are called other than those noted below.
|
|
|
|
// In case you were wondering, no we can't just examine
|
|
|
|
// the tokens because the tokens we have are only the
|
|
|
|
// output of the *first* tokenizer: any line-based
|
|
|
|
// markdown is processed by marked within Parser by
|
|
|
|
// the 'inline lexer'...
|
|
|
|
let is_plain = true;
|
|
|
|
|
|
|
|
function setNotPlain() {
|
|
|
|
is_plain = false;
|
|
|
|
}
|
|
|
|
|
2016-09-22 17:57:46 +00:00
|
|
|
const dummy_renderer = {};
|
2016-09-22 16:18:12 +00:00
|
|
|
for (const k of Object.keys(marked.Renderer.prototype)) {
|
2016-09-22 17:57:46 +00:00
|
|
|
dummy_renderer[k] = setNotPlain;
|
2016-09-22 16:18:12 +00:00
|
|
|
}
|
|
|
|
// text and paragraph are just text
|
2016-09-22 17:57:46 +00:00
|
|
|
dummy_renderer.text = function(t){return t;}
|
|
|
|
dummy_renderer.paragraph = function(t){return t;}
|
2016-09-22 16:18:12 +00:00
|
|
|
|
|
|
|
// ignore links where text is just the url:
|
|
|
|
// this ignores plain URLs that markdown has
|
|
|
|
// detected whilst preserving markdown syntax links
|
2016-09-22 17:57:46 +00:00
|
|
|
dummy_renderer.link = function(href, title, text) {
|
2016-09-22 16:18:12 +00:00
|
|
|
if (text != href) {
|
|
|
|
is_plain = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-22 17:57:46 +00:00
|
|
|
const dummy_options = Object.assign({}, marked_options, {
|
|
|
|
renderer: dummy_renderer,
|
2016-09-22 16:18:12 +00:00
|
|
|
});
|
2016-09-22 17:57:46 +00:00
|
|
|
const dummy_parser = new marked.Parser(dummy_options);
|
|
|
|
dummy_parser.parse(this._copyTokens());
|
2016-09-22 16:18:12 +00:00
|
|
|
|
|
|
|
return is_plain;
|
|
|
|
}
|
|
|
|
|
|
|
|
toHTML() {
|
2016-09-22 17:57:46 +00:00
|
|
|
const real_renderer = new marked.Renderer();
|
|
|
|
real_renderer.link = function(href, title, text) {
|
|
|
|
// prevent marked from turning plain URLs
|
2016-09-23 13:15:48 +00:00
|
|
|
// into links, because its algorithm is fairly
|
2016-09-22 17:57:46 +00:00
|
|
|
// poor. Let's send plain URLs rather than
|
|
|
|
// badly linkified ones (the linkifier Vector
|
|
|
|
// uses on message display is way better, eg.
|
|
|
|
// handles URLs with closing parens at the end).
|
|
|
|
if (text == href) {
|
|
|
|
return href;
|
|
|
|
}
|
2016-09-26 19:09:36 +00:00
|
|
|
return marked.Renderer.prototype.link.apply(this, arguments);
|
2016-09-22 17:57:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
real_renderer.paragraph = (text) => {
|
|
|
|
// The tokens at the top level are the 'blocks', so if we
|
|
|
|
// have more than one, there are multiple 'paragraphs'.
|
|
|
|
// If there is only one top level token, just return the
|
|
|
|
// bare text: it's a single line of text and so should be
|
|
|
|
// 'inline', rather than necessarily wrapped in its own
|
|
|
|
// p tag. If, however, we have multiple tokens, each gets
|
|
|
|
// its own p tag to keep them as separate paragraphs.
|
|
|
|
if (this.tokens.length == 1) {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
return '<p>' + text + '</p>';
|
|
|
|
}
|
|
|
|
|
|
|
|
const real_options = Object.assign({}, marked_options, {
|
|
|
|
renderer: real_renderer,
|
|
|
|
});
|
|
|
|
const real_parser = new marked.Parser(real_options);
|
|
|
|
return real_parser.parse(this._copyTokens());
|
2016-09-22 16:18:12 +00:00
|
|
|
}
|
|
|
|
}
|