Skip to content

Commit

Permalink
ENH Render the new callout block style (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Feb 1, 2024
1 parent 52467ff commit fc34b76
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
12 changes: 6 additions & 6 deletions src/components/CalloutBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import { CalloutBlockProps } from '../types';

const getCalloutClass = (type: string): string => {
switch (type) {
case 'hint':
case 'TIP':
return 'success';
case 'notice':
case 'IMPORTANT':
case 'WARNING':
return 'warning';
case 'alert':
case 'CAUTION':
return 'danger';
case 'note':
case 'NOTE':
return 'info'
default:
return type;
return 'info';
}
};

Expand All @@ -23,7 +24,6 @@ const CalloutBlock: StatelessComponent<CalloutBlockProps> = ({ type, content })
<div className="content">{content}</div>
</div>
);

};

export default CalloutBlock;
4 changes: 4 additions & 0 deletions src/theme/assets/scss/theme/_docs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@
.callout-title {
font-size: 1rem;
}

.content p:last-of-type {
margin-bottom: 0;
}

&.callout-block-info {
border-color: $theme-info-color;
Expand Down
21 changes: 0 additions & 21 deletions src/utils/cleanCalloutTags.ts

This file was deleted.

25 changes: 21 additions & 4 deletions src/utils/parseHTML.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import cleanWhitespace from './cleanWhitespace';
import cleanApiTags from './cleanApiTags';
import rewriteLink from './rewriteLink';
import parseChildrenOf from './parseChildrenOf';
import cleanCalloutTags from './cleanCalloutTags';
import { ReactElement } from 'react';
import rewriteTable from './rewriteTable';
import rewriteHeader from './rewriteHeader';
Expand All @@ -19,7 +18,6 @@ import parseCalloutTags from './parseCalloutTags';
const parseHTML = (html: string): ReactElement | ReactElement[] | string => {
let cleanHTML = html;
cleanHTML = cleanChildrenTags(cleanHTML);
cleanHTML = cleanCalloutTags(cleanHTML);
cleanHTML = cleanWhitespace(cleanHTML);
cleanHTML = cleanApiTags(cleanHTML);
cleanHTML = cleanHeaders(cleanHTML);
Expand All @@ -38,8 +36,27 @@ const parseHTML = (html: string): ReactElement | ReactElement[] | string => {
if (name.match(/^h[0-9]$/)) {
return rewriteHeader(domNode);
}
if (name === 'callout') {
return parseCalloutTags(domNode.attribs.type, domToReact(domNode.children));
if (name === 'blockquote') {
for (const child of children) {
// For some reason blockquotes start with an empty new line with this parser.
if (child.type === 'text' && child.data === "\n") {
continue;
}
// If the first relevant child isn't a paragraph or is empty, it's not a callout block.
if (child.type !== 'tag' || child.name !== 'p' || !child.children?.length || child.children[0].type !== 'text') {
break;
}
// Check if the first text node marks this as a callout block
const firstTextNode = child.children[0];
const calloutTypeRegex = /^\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]\n/;
const matches = firstTextNode.data.match(calloutTypeRegex);
if (!matches) {
break;
}
// Remove the type marker and render the component
firstTextNode.data = firstTextNode.data.replace(calloutTypeRegex, '');
return parseCalloutTags(matches[1], domToReact(children));
}
}
}
if (domNode.data && domNode.parent?.name !== 'code') {
Expand Down

0 comments on commit fc34b76

Please sign in to comment.