-
Notifications
You must be signed in to change notification settings - Fork 192
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: adds prism editor and implements for sdkdocs #581
Changes from 1 commit
0fd6e04
612f90d
3c65a26
b2b00ab
d4741f5
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,109 @@ | ||
/* | ||
|
||
MIT License | ||
|
||
Copyright (c) 2021 Looker Data Sciences, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
*/ | ||
|
||
import React, { FC } from 'react' | ||
import styled from 'styled-components' | ||
import Highlight, { defaultProps, Language } from 'prism-react-renderer' | ||
// import { Prism } from "prism-react-renderer" | ||
import dracula from 'prism-react-renderer/themes/dracula' | ||
|
||
// (typeof global !== "undefined" ? global : window).Prism = Prism | ||
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.
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. For some reason we need to do this for kotlin, c#, swift etc support https://www.npmjs.com/package/prism-react-renderer#faq 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 very familiar with how all things works under the hood. Maybe @bryans99 could chime in on this. Could we somehow lazy load the languages based on the user's sdk language preference? |
||
// require("prismjs/components/prism-kotlin") | ||
// require("prismjs/components/prism-csharp") | ||
// require("prismjs/components/prism-swift") | ||
|
||
interface PrismEditorProps { | ||
language: string | ||
code: string | ||
pattern: string | ||
} | ||
|
||
const Pre = styled.pre` | ||
padding: 1rem; | ||
overflow: auto; | ||
` | ||
|
||
const Line = styled.div` | ||
display: table-row; | ||
` | ||
|
||
const LineNo = styled.span` | ||
display: table-cell; | ||
text-align: right; | ||
padding-right: 1em; | ||
user-select: none; | ||
opacity: 0.5; | ||
` | ||
|
||
const LineContent = styled.span` | ||
display: table-cell; | ||
` | ||
|
||
export const PrismEditor: FC<PrismEditorProps> = ({ | ||
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.
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. Remember that 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. And what if it needs to be used by both ApiExplorer and RunIt? (And DD and Lookml Diagram)? Are there are docs on creating a new packages item? Would this be a good candidate for that? 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. If it needs to be used by both ApiExplorer and RunIt, like in this case, then it should live in RunIt. I don't believe we have any documentation on how to create a new package but I'd be happy to work on that with you |
||
language, | ||
code, | ||
pattern, | ||
}) => { | ||
const getPrismLanguage = (language: string) => { | ||
if (language === 'golang') { | ||
return 'go' | ||
noahamac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return language as Language | ||
} | ||
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 is ok for now, but we can change the generator language/label to |
||
|
||
return ( | ||
<Highlight | ||
{...defaultProps} | ||
code={code} | ||
language={getPrismLanguage(language)} | ||
theme={dracula} | ||
> | ||
{({ className, style, tokens, getLineProps, getTokenProps }) => ( | ||
<Pre className={className} style={style}> | ||
{tokens.map((line, i) => ( | ||
<Line key={i} {...getLineProps({ line, key: i })}> | ||
<LineNo>{i + 1}</LineNo> | ||
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.
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 don't have a preference 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. Line number can be useful for people discussing the code. Let's leave it in until we have people asking to remove it. Or we add a toggle/config setting when we start supporting APIX config settings more fully. |
||
<LineContent> | ||
{line.map((token, key) => { | ||
const tokenProps = getTokenProps({ token, key }) | ||
const text = tokenProps.children | ||
if (pattern !== '' && text.includes(pattern)) { | ||
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.
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 don't think it's bad as is. There are times where it highlights the whole line and is a bit confusing but it's not a big deal. I do think we should open an issue to revisit highlighting across the whole app though because some of it got broken in the latest styling round |
||
tokenProps.style = { | ||
...tokenProps.style, | ||
border: '1px yellow solid', | ||
borderRadius: '4px', | ||
} | ||
} | ||
return <span key={key} {...tokenProps} /> | ||
})} | ||
</LineContent> | ||
</Line> | ||
))} | ||
</Pre> | ||
)} | ||
</Highlight> | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,7 +77,7 @@ export const DocSDKs: FC<LanguageSDKProps> = ({ api, method, type }) => { | |
: gen.declareType('', item as IType) | ||
return ( | ||
<TabPanel key={language}> | ||
<DocCode language={language} code={code} /> | ||
<DocCode language={language} code={code} editor="prism" /> | ||
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. Perhaps the prop should say |
||
</TabPanel> | ||
) | ||
})} | ||
|
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.
Default to
prism
?