File size: 4,138 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | import { MDXProvider } from '@mdx-js/react';
import * as ReactDOM from 'react-dom/server';
import Link from 'next/link';
import Highlight, { defaultProps } from 'prism-react-renderer';
import stripHtml from 'string-strip-html';
import slugify from '@sindresorhus/slugify';
const THEME = {
plain: {
color: '#000',
backgroundColor: 'transparent',
},
styles: [
{
types: ['keyword'],
style: {
color: '#ff0078',
fontWeight: 'bold',
},
},
{
types: ['comment'],
style: {
color: '#999',
fontStyle: 'italic',
},
},
{
types: ['string', 'url', 'attr-value'],
style: {
color: '#028265',
},
},
{
types: ['builtin', 'char', 'constant', 'language-javascript'],
style: {
color: '#000',
},
},
{
types: ['attr-name'],
style: {
color: '#d9931e',
fontStyle: 'normal',
},
},
{
types: ['punctuation', 'operator'],
style: {
color: '#333',
},
},
{
types: ['number', 'function', 'tag'],
style: {
color: '#0076ff',
},
},
{
types: ['boolean', 'regex'],
style: {
color: '#d9931e',
},
},
],
};
// Anchor links
const HeaderLink = ({ tag: Tag, children, ...props }) => {
const slug = slugify(ReactDOM.renderToString(children) || '');
return (
<Tag {...props}>
<span className="subheading-anchor" id={slug} />
<a href={'#' + slug} className="text-current no-underline no-outline">
{children}
<span className="anchor-icon select-none" aria-hidden>
#
</span>
</a>
</Tag>
);
};
const H2 = ({ children, ...props }) => {
return (
<HeaderLink tag="h2" {...props}>
{children}
</HeaderLink>
);
};
const H3 = ({ children, ...props }) => {
return (
<HeaderLink tag="h3" {...props}>
{children}
</HeaderLink>
);
};
const H4 = ({ children, ...props }) => {
return (
<HeaderLink tag="h4" {...props}>
{children}
</HeaderLink>
);
};
const H5 = ({ children, ...props }) => {
return (
<HeaderLink tag="h5" {...props}>
{children}
</HeaderLink>
);
};
const H6 = ({ children, ...props }) => {
return (
<HeaderLink tag="h6" {...props}>
{children}
</HeaderLink>
);
};
const A = ({ children, ...props }) => {
const isExternal = props.href?.startsWith('https://');
if (isExternal) {
return (
<a target="_blank" rel="noopener" {...props}>
{children}
</a>
);
}
return (
<Link href={props.href}>
<a {...props}>{children}</a>
</Link>
);
};
const Code = ({ children, className, highlight, ...props }) => {
if (!className) return <code {...props}>{children}</code>;
const highlightedLines = highlight ? highlight.split(',').map(Number) : [];
// https://mdxjs.com/guides/syntax-highlighting#all-together
const language = className.replace(/language-/, '');
return (
<Highlight
{...defaultProps}
code={children.trim()}
language={language}
theme={THEME}
>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<code className={className} style={{ ...style }}>
{tokens.map((line, i) => (
<div
key={i}
{...getLineProps({ line, key: i })}
style={
highlightedLines.includes(i + 1)
? {
background: '#cce0f5',
margin: '0 -1rem',
padding: '0 1rem',
}
: null
}
>
{line.map((token, key) => (
<span key={key} {...getTokenProps({ token, key })} />
))}
</div>
))}
</code>
)}
</Highlight>
);
};
const components = {
h2: H2,
h3: H3,
h4: H4,
h5: H5,
h6: H6,
a: A,
code: Code,
};
export default ({ children }) => {
return <MDXProvider components={components}>{children}</MDXProvider>;
};
|