Merge pull request #2215 from hmhealey/plt1678
PLT-1678 Refactored how links are extracted to remove code blocks and markdown images
Этот коммит содержится в:
@@ -41,7 +41,7 @@ class PostBody extends React.Component {
|
|||||||
const linkData = Utils.extractLinks(this.props.post.message);
|
const linkData = Utils.extractLinks(this.props.post.message);
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
links: linkData.links,
|
links: linkData,
|
||||||
post: this.props.post
|
post: this.props.post
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -86,10 +86,10 @@ class PostBody extends React.Component {
|
|||||||
componentWillReceiveProps(nextProps) {
|
componentWillReceiveProps(nextProps) {
|
||||||
const linkData = Utils.extractLinks(nextProps.post.message);
|
const linkData = Utils.extractLinks(nextProps.post.message);
|
||||||
if (this.props.post.filenames.length === 0 && this.state.links && this.state.links.length > 0) {
|
if (this.props.post.filenames.length === 0 && this.state.links && this.state.links.length > 0) {
|
||||||
this.embed = this.createEmbed(linkData.links[0]);
|
this.embed = this.createEmbed(linkData[0]);
|
||||||
}
|
}
|
||||||
this.setState({
|
this.setState({
|
||||||
links: linkData.links
|
links: linkData
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -313,41 +313,17 @@ export function getTimestamp() {
|
|||||||
// extracts links not styled by Markdown
|
// extracts links not styled by Markdown
|
||||||
export function extractLinks(text) {
|
export function extractLinks(text) {
|
||||||
const links = [];
|
const links = [];
|
||||||
let replaceText = text;
|
let inText = text;
|
||||||
|
|
||||||
// pull out the Markdown code blocks
|
// strip out code blocks
|
||||||
const codeBlocks = [];
|
inText = inText.replace(/`[^`]*`/g, '');
|
||||||
const splitText = replaceText.split('`'); // also handles ```
|
|
||||||
for (let i = 1; i < splitText.length; i += 2) {
|
// strip out inline markdown images
|
||||||
if (splitText[i].trim() !== '') {
|
inText = inText.replace(/!\[[^\]]*\]\([^\)]*\)/g, '');
|
||||||
codeBlocks.push(splitText[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function replaceFn(autolinker, match) {
|
function replaceFn(autolinker, match) {
|
||||||
let link = '';
|
let link = '';
|
||||||
const matchText = match.getMatchedText();
|
const matchText = match.getMatchedText();
|
||||||
const tempText = replaceText;
|
|
||||||
|
|
||||||
const start = replaceText.indexOf(matchText);
|
|
||||||
const end = start + matchText.length;
|
|
||||||
|
|
||||||
replaceText = replaceText.substring(0, start) + replaceText.substring(end);
|
|
||||||
|
|
||||||
// if it's a Markdown link, just skip it
|
|
||||||
if (start > 1) {
|
|
||||||
if (tempText.charAt(start - 2) === ']' && tempText.charAt(start - 1) === '(' && tempText.charAt(end) === ')') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// if it's in a Markdown code block, skip it
|
|
||||||
for (const i in codeBlocks) {
|
|
||||||
if (codeBlocks[i].indexOf(matchText) === 0) {
|
|
||||||
codeBlocks[i] = codeBlocks[i].replace(matchText, '');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (matchText.trim().indexOf('http') === 0) {
|
if (matchText.trim().indexOf('http') === 0) {
|
||||||
link = matchText;
|
link = matchText;
|
||||||
@@ -358,16 +334,19 @@ export function extractLinks(text) {
|
|||||||
links.push(link);
|
links.push(link);
|
||||||
}
|
}
|
||||||
|
|
||||||
Autolinker.link(text, {
|
Autolinker.link(
|
||||||
replaceFn,
|
inText,
|
||||||
urls: {schemeMatches: true, wwwMatches: true, tldMatches: false},
|
{
|
||||||
emails: false,
|
replaceFn,
|
||||||
twitter: false,
|
urls: {schemeMatches: true, wwwMatches: true, tldMatches: false},
|
||||||
phone: false,
|
emails: false,
|
||||||
hashtag: false
|
twitter: false,
|
||||||
});
|
phone: false,
|
||||||
|
hashtag: false
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
return {links, text};
|
return links;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function escapeRegExp(string) {
|
export function escapeRegExp(string) {
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user