Skip to content

Commit

Permalink
Move to use across any text node
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 committed Nov 10, 2023
1 parent 040de7e commit 304da23
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
17 changes: 16 additions & 1 deletion packages/myst-to-react/src/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,22 @@ type BasicNodeRenderers = {

const BASIC_RENDERERS: BasicNodeRenderers = {
text({ node }) {
return <>{node.value}</>;
// Change zero-width space into `<wbr>` which is better for copying
// These are used in links, and potentially other long words
if (!node.value?.includes('​')) {
return <>{node.value}</>;
}
const text = node.value.split('​');
return (
<>
{text.map((v, i) => (
<>
{v}
{i < text.length - 1 && <wbr />}
</>
))}
</>
);
},
span({ node }) {
// style={node.style}
Expand Down
30 changes: 4 additions & 26 deletions packages/myst-to-react/src/links/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,6 @@ function InternalLink({ url, children }: { url: string; children: React.ReactNod
);
}

/** This changes zero-width space into `<wbr>` which is better for copying */
const FormattedLinkText: NodeRenderer<TransformedLink> = ({ node }) => {
if (
node.children?.length !== 1 ||
node.children[0].type !== 'text' ||
!node.children[0].value.includes('​')
) {
return <MyST ast={node.children} />;
}
const text = node.children[0].value.split('​');
return (
<>
{text.map((v, i) => (
<>
{v}
{i < text.length - 1 && <wbr />}
</>
))}
</>
);
};

export const link: NodeRenderer<TransformedLink> = ({ node }) => {
const internal = node.internal ?? false;
const protocol = node.protocol;
Expand All @@ -84,7 +62,7 @@ export const link: NodeRenderer<TransformedLink> = ({ node }) => {
case 'wiki':
return (
<WikiLink url={node.url} page={node.data?.page as string} wiki={node.data?.wiki as string}>
<FormattedLinkText node={node} />
<MyST ast={node.children} />
</WikiLink>
);
case 'github':
Expand All @@ -100,7 +78,7 @@ export const link: NodeRenderer<TransformedLink> = ({ node }) => {
to={node.data?.to as number | undefined}
issue_number={node.data?.issue_number as number | undefined}
>
<FormattedLinkText node={node} />
<MyST ast={node.children} />
</GithubLink>
);
case 'rrid':
Expand All @@ -109,13 +87,13 @@ export const link: NodeRenderer<TransformedLink> = ({ node }) => {
if (internal) {
return (
<InternalLink url={node.url}>
<FormattedLinkText node={node} />
<MyST ast={node.children} />
</InternalLink>
);
}
return (
<a target="_blank" href={node.url} rel="noreferrer">
<FormattedLinkText node={node} />
<MyST ast={node.children} />
</a>
);
}
Expand Down

0 comments on commit 304da23

Please sign in to comment.