-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from pluralsh/klink/content-updates
feat: Content updates
- Loading branch information
Showing
12 changed files
with
169 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import React, { useMemo } from 'react' | ||
|
||
import { InlineCode, isExternalUrl } from '@pluralsh/design-system' | ||
import Link from 'next/link' | ||
|
||
import Markdoc, { nodes } from '@markdoc/markdoc' | ||
|
||
import { BasicP, BasicUl, InlineLink } from './Typography' | ||
|
||
const transformConfig = ({ renderP = true }: { renderP?: boolean }) => ({ | ||
nodes: { | ||
document: { ...nodes.document, render: 'NoWrapper' }, | ||
list: { ...nodes.list, render: 'List' }, | ||
paragraph: { ...nodes.paragraph, render: renderP ? 'P' : 'NoWrapper' }, | ||
link: { ...nodes.link, render: 'Link' }, | ||
code: { ...nodes.code, render: 'Code' }, | ||
}, | ||
}) | ||
|
||
const renderOpts = { | ||
components: { | ||
// eslint-disable-next-line react/jsx-no-useless-fragment | ||
NoWrapper: ({ children }) => <>{children}</>, | ||
List: BasicUl, | ||
P: (props) => <BasicP {...props} />, | ||
Link: (props) => ( | ||
<InlineLink | ||
as={Link} | ||
{...(isExternalUrl(props.href) | ||
? { target: '_blank', rel: 'noopener noreferrer' } | ||
: {})} | ||
{...(props as any)} | ||
/> | ||
), | ||
Code: InlineCode, | ||
}, | ||
} | ||
|
||
export function BasicMarkdoc({ | ||
text, | ||
variables, | ||
renderP = true, | ||
}: { | ||
text?: string | null | ||
variables?: Record<string, any> | ||
renderP?: boolean | ||
}) { | ||
text = text?.replace(/^\s+|\s+$/g, '') | ||
const content = useMemo(() => { | ||
if (!text) { | ||
return null | ||
} | ||
const ast = Markdoc.parse(text) | ||
|
||
return Markdoc.transform(ast, { | ||
...transformConfig({ renderP }), | ||
variables, | ||
}) | ||
}, [renderP, text, variables]) | ||
|
||
if (!content) { | ||
return null | ||
} | ||
|
||
return <>{Markdoc.renderers.react(content, React, renderOpts)}</> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.