Skip to content

Commit

Permalink
Fix bug Code block tabs broke the referenced links
Browse files Browse the repository at this point in the history
- The reason is that the previous Codeblock implementation separates
the tabs, the markdown before tabs, and the markdown after tabs into
separate Remarkable component, thus they don't share information
regarding the reference link
- To solve this, change the Doc implementation so that one Doc
have only one Remarkable component by transforming the codeblock
into html string and add it as part of the markdown, letting the
Remarkable take care of the html string
- However, this approach made us need to ensure that there is no
newline in the codetab, otherwise, the formatting inside the
code will be broken. Thus, I replace every newline inside the
code tag with a br tag

Fix #1215
  • Loading branch information
fiennyangeln committed Feb 26, 2019
1 parent 8375236 commit 9f4a528
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions v1/lib/core/Doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

const React = require('react');
const { renderToStaticMarkup } = require('react-dom/server');
const MarkdownBlock = require('./MarkdownBlock.js');
const CodeTabsMarkdownBlock = require('./CodeTabsMarkdownBlock.js');

Expand All @@ -31,6 +32,28 @@ const splitTabsToTitleAndContent = content => {
}));
};

const cleanTheCodeTag = content => {
const contents = content.split(
/(<pre>)(.*?)(<\/pre>)/gms,
);
let inCodeBlock = false;
const cleanContents = contents.map((c) => {
if (c === '<pre>') {
inCodeBlock = true;
return c;
} if (c === '</pre>') {
inCodeBlock = false;
return c;
} if (inCodeBlock) {
return c.replace(/\n/g, "<br />");
}
return c;


});
return cleanContents.join('');
};

// inner doc component for article itself
class Doc extends React.Component {
renderContent() {
Expand All @@ -40,26 +63,27 @@ class Doc extends React.Component {
/(<!--DOCUSAURUS_CODE_TABS-->\n)(.*?)(\n<!--END_DOCUSAURUS_CODE_TABS-->)/gms,
);

const renderResult = contents.map((c, index) => {
const renderResult = contents.map((c) => {
if (c === '<!--DOCUSAURUS_CODE_TABS-->\n') {
inCodeTabs = true;
return null;
return '';
}
if (c === '\n<!--END_DOCUSAURUS_CODE_TABS-->') {
inCodeTabs = false;
return null;
return '';
}
if (inCodeTabs) {
return (
const codeTabsMarkdownBlock = renderToStaticMarkup(
<CodeTabsMarkdownBlock>
{splitTabsToTitleAndContent(c)}
</CodeTabsMarkdownBlock>
);
return cleanTheCodeTag(codeTabsMarkdownBlock);
}
return <MarkdownBlock key={index}>{c}</MarkdownBlock>;
return c;
});

return renderResult;
return renderResult.join('');
}

render() {
Expand Down Expand Up @@ -110,7 +134,9 @@ class Doc extends React.Component {
<h1 className="postHeaderTitle">{this.props.title}</h1>
)}
</header>
<article>{this.renderContent()}</article>
<article>
<MarkdownBlock>{this.renderContent()}</MarkdownBlock>
</article>
</div>
);
}
Expand Down

0 comments on commit 9f4a528

Please sign in to comment.