-
Notifications
You must be signed in to change notification settings - Fork 40
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 #122 from CooperRedhat/markdown-admonitions
Add showdown extension for admonitions through markdown syntax
- Loading branch information
Showing
5 changed files
with
103 additions
and
1 deletion.
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
66 changes: 66 additions & 0 deletions
66
...ages/module/src/ConsoleShared/src/components/markdown-extensions/admonition-extension.tsx
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 * as React from 'react'; | ||
import { removeTemplateWhitespace } from './utils'; | ||
import { renderToStaticMarkup } from 'react-dom/server'; | ||
import { Alert } from '@patternfly/react-core'; | ||
import LightbulbIcon from '@patternfly/react-icons/dist/js/icons/lightbulb-icon'; | ||
import FireIcon from '@patternfly/react-icons/dist/js/icons/fire-icon'; | ||
import './showdown-extension.scss'; | ||
|
||
enum AdmonitionType { | ||
TIP = 'TIP', | ||
NOTE = 'NOTE', | ||
IMPORTANT = 'IMPORTANT', | ||
WARNING = 'WARNING', | ||
CAUTION = 'CAUTION', | ||
} | ||
|
||
const admonitionToAlertVariantMap = { | ||
[AdmonitionType.NOTE]: { variant: 'info' }, | ||
[AdmonitionType.TIP]: { variant: 'default', customIcon: <LightbulbIcon /> }, | ||
[AdmonitionType.IMPORTANT]: { variant: 'danger' }, | ||
[AdmonitionType.CAUTION]: { variant: 'warning', customIcon: <FireIcon /> }, | ||
[AdmonitionType.WARNING]: { variant: 'warning' }, | ||
}; | ||
|
||
const useAdmonitionShowdownExtension = () => { | ||
// const { getResource } = React.useContext<QuickStartContextValues>(QuickStartContext); | ||
return React.useMemo( | ||
() => ({ | ||
type: 'lang', | ||
regex: /\[([\d\w\s-()$!]+)]{{(admonition) ([\w-]+)}}/g, | ||
replace: ( | ||
text: string, | ||
content: string, | ||
admonitionLabel: string, | ||
admonitionType: string, | ||
groupId: string, | ||
): string => { | ||
if (!content || !admonitionLabel || !admonitionType || !groupId) { | ||
return text; | ||
} | ||
admonitionType = admonitionType.toUpperCase(); | ||
|
||
const { variant, customIcon } = admonitionToAlertVariantMap[admonitionType]; | ||
const style = | ||
admonitionType === AdmonitionType.CAUTION ? { backgroundColor: '#ec7a0915' } : {}; | ||
|
||
const pfAlert = ( | ||
<Alert | ||
variant={variant} | ||
customIcon={customIcon && customIcon} | ||
isInline | ||
title={admonitionType} | ||
className="pfext-markdown-admonition" | ||
style={style} | ||
> | ||
{content} | ||
</Alert> | ||
); | ||
return removeTemplateWhitespace(renderToStaticMarkup(pfAlert)); | ||
}, | ||
}), | ||
[], | ||
); | ||
}; | ||
|
||
export default useAdmonitionShowdownExtension; |
1 change: 1 addition & 0 deletions
1
packages/module/src/ConsoleShared/src/components/markdown-extensions/index.ts
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { default as MarkdownCopyClipboard } from './MarkdownCopyClipboard'; | ||
export { default as useInlineCopyClipboardShowdownExtension } from './inline-clipboard-extension'; | ||
export { default as useMultilineCopyClipboardShowdownExtension } from './multiline-clipboard-extension'; | ||
export { default as useAdmonitionShowdownExtension } from './admonition-extension'; |
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