Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make referenced links work with code block tabs #1249

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 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,26 @@ 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 +61,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>
</CodeTabsMarkdownBlock>,
);
return cleanTheCodeTag(codeTabsMarkdownBlock);
}
return <MarkdownBlock key={index}>{c}</MarkdownBlock>;
return c;
});

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

render() {
Expand Down Expand Up @@ -110,7 +132,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