Moved text formatting code to only occur inside of markdown text nodes
Этот коммит содержится в:
@@ -1,9 +1,18 @@
|
|||||||
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
|
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
|
||||||
// See License.txt for license information.
|
// See License.txt for license information.
|
||||||
|
|
||||||
|
const TextFormatting = require('./text_formatting.jsx');
|
||||||
|
|
||||||
const marked = require('marked');
|
const marked = require('marked');
|
||||||
|
|
||||||
export class MattermostMarkdownRenderer extends marked.Renderer {
|
export class MattermostMarkdownRenderer extends marked.Renderer {
|
||||||
|
constructor(options, formattingOptions = {}) {
|
||||||
|
super(options);
|
||||||
|
|
||||||
|
this.text = this.text.bind(this);
|
||||||
|
|
||||||
|
this.formattingOptions = formattingOptions;
|
||||||
|
}
|
||||||
link(href, title, text) {
|
link(href, title, text) {
|
||||||
let outHref = href;
|
let outHref = href;
|
||||||
|
|
||||||
@@ -19,4 +28,8 @@ export class MattermostMarkdownRenderer extends marked.Renderer {
|
|||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
text(text) {
|
||||||
|
return TextFormatting.doFormatText(text, this.formattingOptions);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,8 +10,6 @@ const Utils = require('./utils.jsx');
|
|||||||
|
|
||||||
const marked = require('marked');
|
const marked = require('marked');
|
||||||
|
|
||||||
const markdownRenderer = new Markdown.MattermostMarkdownRenderer();
|
|
||||||
|
|
||||||
// Performs formatting of user posts including highlighting mentions and search terms and converting urls, hashtags, and
|
// Performs formatting of user posts including highlighting mentions and search terms and converting urls, hashtags, and
|
||||||
// @mentions to links by taking a user's message and returning a string of formatted html. Also takes a number of options
|
// @mentions to links by taking a user's message and returning a string of formatted html. Also takes a number of options
|
||||||
// as part of the second parameter:
|
// as part of the second parameter:
|
||||||
@@ -25,14 +23,30 @@ export function formatText(text, options = {}) {
|
|||||||
options.markdown = true;
|
options.markdown = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait until marked can sanitize the html so that we don't break markdown block quotes
|
|
||||||
let output;
|
let output;
|
||||||
if (!options.markdown) {
|
|
||||||
output = sanitizeHtml(text);
|
if (options.markdown) {
|
||||||
|
// the markdown renderer will call doFormatText as necessary so just call marked
|
||||||
|
output = marked(text, {
|
||||||
|
renderer: new Markdown.MattermostMarkdownRenderer(null, options),
|
||||||
|
sanitize: true
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
output = text;
|
output = sanitizeHtml(text);
|
||||||
|
output = doFormatText(output, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// replace newlines with spaces if necessary
|
||||||
|
if (options.singleline) {
|
||||||
|
output = replaceNewlines(output);
|
||||||
|
}
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function doFormatText(text, options) {
|
||||||
|
let output = text;
|
||||||
|
|
||||||
const tokens = new Map();
|
const tokens = new Map();
|
||||||
|
|
||||||
// replace important words and phrases with tokens
|
// replace important words and phrases with tokens
|
||||||
@@ -52,22 +66,9 @@ export function formatText(text, options = {}) {
|
|||||||
output = highlightCurrentMentions(output, tokens);
|
output = highlightCurrentMentions(output, tokens);
|
||||||
}
|
}
|
||||||
|
|
||||||
// perform markdown parsing while we have an html-free input string
|
|
||||||
if (options.markdown) {
|
|
||||||
output = marked(output, {
|
|
||||||
renderer: markdownRenderer,
|
|
||||||
sanitize: true
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// reinsert tokens with formatted versions of the important words and phrases
|
// reinsert tokens with formatted versions of the important words and phrases
|
||||||
output = replaceTokens(output, tokens);
|
output = replaceTokens(output, tokens);
|
||||||
|
|
||||||
// replace newlines with html line breaks
|
|
||||||
if (options.singleline) {
|
|
||||||
output = replaceNewlines(output);
|
|
||||||
}
|
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user