Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help with autolinking #275

Open
MikeWillis opened this issue Feb 17, 2025 · 2 comments
Open

Help with autolinking #275

MikeWillis opened this issue Feb 17, 2025 · 2 comments

Comments

@MikeWillis
Copy link

I'm sorry but I'm confused by the documentation. Can anyone point me in the right direction for autolinking content pulled from my wordpress blog? I'm using react on the front end, and wordpress' built-in json api to pull the html of each blog post.

I won't know the link text or link url beforehand, obviously. I would like to use interweave to parse the incoming html and turn it into a jsx element, and replace the anchor tags with my own component. Can that be done?

For example something like:

import { Markup, Interweave } from 'interweave';
import { UrlMatcher } from 'interweave-autolink';

import MyOwnLinkComponent from "somewhere";

const MyComponent = props => {
	
	let ajaxContent = "<p>this is a blog post with a <a href='https://www.example.com/wherever/'>link somewhere</a>";

	return (
		<div>
			<Interweave
				content={ajaxContent}
				matchers={[new UrlMatcher(ajaxContent)]}
			/>
		</div>
	);
};

with the idea being that the anchor tag in the incoming content gets replaced with something like:

<MyOwnLinkComponent to={href}>{linkText}</MyOwnLinkComponent>

@milesj
Copy link
Owner

milesj commented Feb 17, 2025

@MikeWillis Autolinking just wraps URLs with a tags. You need to use transform to actually replace them: https://interweave.dev/docs/parser#replacing-elements

@MikeWillis
Copy link
Author

@milesj that's what I was missing, thanks!

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

No branches or pull requests

2 participants