Skip to content

Conversation

jonathanatger
Copy link

First, thanks for your time on this project. It is the best markdown converter for React in my opinion due to its light weight.

I am not familiar with open source contributing and yarn, hopefully the PR still conveys the (simple) idea and code changes, even though contributing guidelines are not fully met.

The change allows for the ability to add the target="_blank" on anchor tags, and have them open in a new tag upon click. This makes the links a lot more usable in certain contexts, notably AI generated markdown with links.

Copy link

changeset-bot bot commented Apr 2, 2025

⚠️ No Changeset found

Latest commit: 1fb1a27

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

: namedCodesToUnicode

options.createElement = options.createElement || React.createElement
options.targetBlank = options.targetBlank || false

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than a boolean for a single use case, a better option would be to provide a string, and the options could specify a target window.

Copy link
Owner

@quantizor quantizor Apr 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way I'd probably accomplish this is more like:

options={{overrides: {a: {props: {target: '_blank'}}}}}

key={state.key}
href={options.sanitizer(node.target, 'a', 'href')}
title={node.title}
target={options.targetBlank ? '_blank' : undefined}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if targetBlank were a string | undefined, we wouldn't need to do checks here and just set target to options.target

@hunkydoryrepair
Copy link

IMO, this would be nice, but in many cases we need to override it anyway. Opening a new window is a common use case, but this can be handled pretty easily with overrides. This is what mine does, where openLink runs javascript to create a UI confirmation dialog about linking outside the current application:

const Link = ({ href, children }: { href: string; children: React.ReactElement }) => {
  return <a onClick={() => openLink(href)}>{children}</a>;
};

...

return compiler(md, {
    overrides: {
      a: Link,
    },
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants