-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
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
feat: code block tab #1063
feat: code block tab #1063
Changes from 4 commits
dc88757
1826622
706b095
c54abee
b9ab3d2
261e8e7
8458a9f
9cb10e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import _ from 'lodash'; | ||
|
||
const React = require('react'); | ||
const Remarkable = require('./Remarkable'); | ||
|
||
/** | ||
* The MarkdownBlock component is used to parse markdown and render to HTML. | ||
*/ | ||
class MarkdownBlock extends React.Component { | ||
render() { | ||
const groupId = _.uniqueId(); | ||
|
||
const tabs = this.props.children.map(({title, content}) => ({ | ||
id: _.uniqueId(), | ||
groupId: groupId, | ||
label: title, | ||
lang: title, | ||
panelContent: <Remarkable source={content} />, | ||
})); | ||
|
||
return ( | ||
<div className="tabs"> | ||
<div className="nav-tabs"> | ||
{tabs.map((t, i) => ( | ||
<div | ||
className={`nav-link${i === 0 ? ' active' : ''}`} | ||
id={`${t.id}-tab`} | ||
data-group={`group_${t.groupId}`} | ||
data-tab={`tabpanel_${t.id}`}> | ||
{t.label} | ||
</div> | ||
))} | ||
</div> | ||
<div className="tab-content"> | ||
{tabs.map((t, i) => ( | ||
<div | ||
className={`tab-pane${i === 0 ? ' active' : ''}`} | ||
data-group={`group_${t.groupId}`} | ||
tabIndex="-1" | ||
id={`tabpanel_${t.id}`}> | ||
{t.panelContent} | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
module.exports = MarkdownBlock; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,4 +44,25 @@ window.addEventListener('load', function() { | |
textEl.textContent = 'Copy'; | ||
}, 2000); | ||
}); | ||
|
||
// add event listener for all tab | ||
document.querySelectorAll('.nav-link').forEach(function(el) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will need to make sure that this is installed as part of the Docusaurus install process -- e.g., |
||
el.addEventListener('click', function(e) { | ||
const groupId = e.target.getAttribute('data-group'); | ||
document | ||
.querySelectorAll(`.nav-link[data-group=${groupId}]`) | ||
.forEach(function(el) { | ||
el.classList.remove('active'); | ||
}); | ||
document | ||
.querySelectorAll(`.tab-pane[data-group=${groupId}]`) | ||
.forEach(function(el) { | ||
el.classList.remove('active'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldnt be placed here because not every docusaurus user has this file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @endiliey I suggested that we add it as part of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm yeah I'm confused where to put it actually 😯 I put it there to mimic the code blocks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should I make something like this? https://gist.github.com/yangshun/55db997ed0f8f4e6527571fc3bee4675 and put all the related css outside of the lib? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. put it in v1/lib/static and modify the Head.js to always include it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @endiliey @JoelMarcey made the changes:D please review! |
||
}); | ||
e.target.classList.add('active'); | ||
document | ||
.querySelector(`#${e.target.getAttribute('data-tab')}`) | ||
.classList.add('active'); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,12 +34,108 @@ root-of-repo | |
│ └── static | ||
``` | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add this documentation to https://github.com/facebook/Docusaurus/blob/master/docs/api-doc-markdown.md instead? |
||
## Codeblocks in multiple languages | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add some wording about what is going on here. |
||
DOCUSAURUS_CODE_TABS | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a clever way to do this. |
||
TAB_TITLE=Javascript | ||
```js | ||
const a = 33 + 4; | ||
console.log(a); | ||
``` | ||
END_TAB | ||
TAB_TITLE=Python | ||
```py | ||
a = 33 + 4 | ||
print(a) | ||
``` | ||
END_TAB | ||
END_DOCUSAURUS_CODE_TABS | ||
|
||
Of course, you are also free to write your own pages. It is strongly suggested that you at least have an index page, but none of the pages provided are mandatory to include in your site. More information on how to use the provided components or include your own can be found [here](api-pages.md). Information on how to link to your different pages in the header navigation bar can be found [here](guides-navigation.md). | ||
|
||
> If you want your page to show up in your navigation header, you will need to update `siteConfig.js` to add to the `headerLinks` element. e.g., `{ page: 'about-slash', label: 'About/' }`, | ||
|
||
## Adding Static Pages | ||
|
||
DOCUSAURUS_CODE_TABS | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure you need this all this example here if you add the documentation to https://github.com/facebook/Docusaurus/blob/master/docs/api-doc-markdown.md |
||
TAB_TITLE=Javascript | ||
```js | ||
const a = 33 + 4; | ||
``` | ||
END_TAB | ||
TAB_TITLE=Python | ||
```py | ||
a = 33 + 4; | ||
``` | ||
END_TAB | ||
TAB_TITLE=React | ||
```jsx | ||
const React = require('react'); | ||
|
||
class HelloWorld extends React.Component { | ||
render() { | ||
return <p>Hello, World!</p>; | ||
} | ||
} | ||
``` | ||
END_TAB | ||
TAB_TITLE=Java | ||
```java | ||
public class Main { | ||
public static void main(String[] args) { | ||
System.out.println("Hello, World!"); | ||
} | ||
} | ||
``` | ||
END_TAB | ||
TAB_TITLE=C++ | ||
```java | ||
public class Main { | ||
public static void main(String[] args) { | ||
System.out.println("Hello, World!"); | ||
} | ||
} | ||
``` | ||
END_TAB | ||
TAB_TITLE=C | ||
```java | ||
public class Main { | ||
public static void main(String[] args) { | ||
System.out.println("Hello, World!"); | ||
} | ||
} | ||
``` | ||
END_TAB | ||
TAB_TITLE=C# | ||
```java | ||
public class Main { | ||
public static void main(String[] args) { | ||
System.out.println("Hello, World!"); | ||
} | ||
} | ||
``` | ||
END_TAB | ||
TAB_TITLE=LongName | ||
```java | ||
public class Main { | ||
public static void main(String[] args) { | ||
System.out.println("Hello, World!"); | ||
} | ||
} | ||
``` | ||
END_TAB | ||
TAB_TITLE=Long name | ||
```java | ||
public class Main { | ||
public static void main(String[] args) { | ||
System.out.println("Hello, World!"); | ||
} | ||
} | ||
``` | ||
END_TAB | ||
END_DOCUSAURUS_CODE_TABS | ||
|
||
Static `.html` files can also be used, but they will not include Docusaurus' header, footer, or styles by default. These can be added to the `static` folder in the same way as other [static assets](api-pages.md#using-static-assets). Alternatively, they can be placed in the `pages` folder and would be served as-is instead of being rendered from React. | ||
|
||
If you wish to use Docusaurus' stylesheet, you can access it at `${baseUrl}css/main.css`. If you wish to use separate css for these static pages, you can exclude them from being concatenated to Docusaurus' styles by adding them into the `siteConfig.separateCss` field in `siteConfig.js`. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove these.