Skip to content
Snippets Groups Projects
Commit c85d9592 authored by Quoc Huy Nguyen Dinh's avatar Quoc Huy Nguyen Dinh
Browse files

Refactor the embedded player code injection logic in order to not change the initial structure

parent c399af78
No related branches found
No related tags found
2 merge requests!354Remove linkify:false from Remarkable call. The option has been removed from...,!352Refactor the embedded player code injection logic in order to not change the initial structure
import React, { Component } from 'react'; import React, { Component } from 'react';
import { renderToString } from 'react-dom/server';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { Remarkable } from 'remarkable'; import { Remarkable } from 'remarkable';
...@@ -120,37 +121,21 @@ class MarkdownViewer extends Component { ...@@ -120,37 +121,21 @@ class MarkdownViewer extends Component {
const noImageActive = cleanText.indexOf(noImageText) !== -1; const noImageActive = cleanText.indexOf(noImageText) !== -1;
const regex = /~~~ embed:(.*? ~~~)/gm;
let matches;
let processedText = cleanText;
// In addition to inserting the youtube component, this allows // In addition to inserting the youtube component, this allows
// react to compare separately preventing excessive re-rendering. // react to compare separately preventing excessive re-rendering.
let idx = 0; let idx = 0;
const sections = [];
function checksum(s) { while ((matches = regex.exec(processedText)) !== null) {
let chk = 0x12345678; // This is necessary to avoid infinite loops with zero-width matches
const len = s.length; if (matches.index === regex.lastIndex) {
for (let i = 0; i < len; i += 1) { regex.lastIndex += 1;
chk += s.charCodeAt(i) * (i + 1);
} }
// eslint-disable-next-line no-bitwise const embedMd = EmbeddedPlayerGenerateMd(matches[1], idx, large);
return (chk & 0xffffffff).toString(16); processedText = processedText.replace(matches[0], renderToString(embedMd.markdown));
}
// HtmlReady inserts ~~~ embed:${id} type ~~~
for (let section of cleanText.split('~~~ embed:')) {
const embedMd = EmbeddedPlayerGenerateMd(section, idx, large);
if (embedMd) {
const { section: newSection, markdown } = embedMd;
section = newSection;
sections.push(markdown);
if (section === '') {
// eslint-disable-next-line no-continue
continue;
}
}
sections.push(<div key={checksum(section)} dangerouslySetInnerHTML={{ __html: section }} />);
idx += 1; idx += 1;
} }
...@@ -162,7 +147,7 @@ class MarkdownViewer extends Component { ...@@ -162,7 +147,7 @@ class MarkdownViewer extends Component {
return ( return (
<div className={'MarkdownViewer ' + cn}> <div className={'MarkdownViewer ' + cn}>
{sections} <div dangerouslySetInnerHTML={{ __html: processedText }} />
{noImageActive && allowNoImage && ( {noImageActive && allowNoImage && (
<div <div
role="link" role="link"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment