-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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(v2): add filename in CodeBlock #2346
feat(v2): add filename in CodeBlock #2346
Conversation
Deploy preview for docusaurus-2 ready! Built with commit 13e024f |
.fileName { | ||
padding: 4px 12px; | ||
font-weight: bold; | ||
text-decoration: underline; |
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.
I think it would be better to separate these two blocks via the bottom border.
code title
code
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.
@@ -40,6 +40,7 @@ export default ({children, className: languageClassName, metastring}) => { | |||
|
|||
const target = useRef(null); | |||
const button = useRef(null); | |||
const fileName = languageClassName.split(':')[1]; |
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.
Can we make this feature more general? It seems to me that we need to look at this as a code title, not as just a code filename. For example, I want to add some text that has spaces in it, will it work?
```js:"My Code title" {2}
console.log('123');
```
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.
Agree! And currently your example not working 😢
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.
Could you add such an opportunity?
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.
sure!
@lex111 Hi, I update code! Thanks for your review. 1. To use Regex to find title from markdown. I'm not mature to write clean code to accomplish the below syntax 😢
Current mdx api is not good for above syntax. That's why I change markdown syntax like below. (order doesn't matter.)
2. Change design style based on your advice
Thanks! |
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.
@kohheepeace thanks for rework!
Remember to also duplicate these changes in live codeblock https://github.com/facebook/docusaurus/blob/a2fb0648340778428880e19e88a2aa84b1154329/packages/docusaurus-theme-live-codeblock/src/index.js
And as a demo add a code title in any code block to website/docs/
(for "package.json" here eg)
<button | ||
ref={button} | ||
type="button" | ||
aria-label="Copy code to clipboard" | ||
className={styles.copyButton} | ||
className={classnames(styles.copyButton, { | ||
[`${styles.copyButtonWithTitle}`]: codeBlockTitle, |
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.
Just use [styles.copyButtonWithTitle]: codeBlockTitle,
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.
Oh okay! Nice!
packages/docusaurus-theme-classic/src/theme/CodeBlock/styles.module.css
Outdated
Show resolved
Hide resolved
@lex111 I will re-PR by end of today with |
@lex111 I updated code 👍 |
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.
Hi @kohheepeace sorry for the long answer, but we need to remove a fixed height for the code title, since on mobiles we observe the following:
Current | Should be |
---|---|
I think it is possible, as it was earlier, to move copy button to the pre
element and position this button accordingly with respect to the pre
element.
@lex111 thanks for taking time for review! I will fix this asap. sorry ! |
@lex111 I updated code.
Just by doing 👆 leads to horizontal scroll bug like below. (It worked before because That's why I also need to separate code block and title. 😢 |
@kohheepeace most likely yes, in this situation, an additional container is indispensable. Although perhaps @yangshun can tell an alternative solution. In the meantime, let's just rename And also make copy button appear when you hover over code title with this code: .codeBlockTitle:hover + .codeBlockContent .copyButton,
.codeBlockContent:hover > .copyButton {
visibility: visible;
opacity: 1;
} |
=> super agreed 👍
=>okay 👍 |
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.
Little docs improvements.
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.
👍
@yangshun please review.
Would love to see this get in so we can use it in the Redux docs! |
@kohheepeace I updated the code blocks markup in |
@lex111 okay! Should I rebase or merge commit? |
@kohheepeace the usual (merge) commit will be enough. |
- Delete unnecessary template literals - Delete unnecessary "important!" from css
2a2e80a
to
13e024f
Compare
@lex111 sorry for my poor git knowledge. I fixed conflict. |
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.
Awesome, thank you!
yayyy! #2456 next? :) |
Hi, I'm having some problems getting this to work on my end. The title doesn't show up when I'm copying this code from the docs. https://v2.docusaurus.io/docs/markdown-features#code-title
I'm on version 2.0.0-alpha.50. Not very experienced with web dev. |
Are you sure you're using the latest version (eg. try reinstall deps)? I see no other reason why it can't work. We need the repository so that we can reproduce this issue. |
Hi lex111, no I'm not sure. I tried following the upgrade instructions. When you hinted that I might be on the wrong version, I checked my package.json file which I've already updated.
I changed so that all the versions were alpha.50, not just the core and preset-classic. That fixed the title issue I had for the code blocks, at least they show up now. But there is some issue with the styling, no rounded corner and can't read the copy button text when mouseover. I'll push my code to my repo, I'd be willing to try to reinstall reps, how can I do that? yarn install? That gives me a few warning |
@OlofHarrysson we are aware of this issue and have already fixed it, so we will soon make a new release that will also include this fix. |
@lex111 Ok great! I'll wait for the next release and work on my content in the meantime. Thanks for your help |
Motivation
Add filename in CodeBlock which is mentioned in #2271
If you write below markdown like below, it will create filename in CodeBlock.
Screenshot of output
[dark mode]
[white mode]
I like this markdown syntax to create "filename", but it depends on docusaurus team.
Have you read the Contributing Guidelines on pull requests?
Yes!
Test Plan
Add CodeBlock with this syntax in your doc.