editor.js icon indicating copy to clipboard operation
editor.js copied to clipboard

Feature: auto link

Open collimarco opened this issue 1 year ago • 2 comments
trafficstars

It would be nice that when someone enters / pastes a link in the text, that URL becomes automatically a link.

Actual: the URL remains normal text

Expected: the URL becomes a link automatically

collimarco avatar Jun 06 '24 09:06 collimarco

i hope so too

kaaaaaaaaaaai avatar Jun 13 '24 01:06 kaaaaaaaaaaai

I've added this feature using a small trick

  • Detect the editorjs blocks such as paragraph, list and link
  • update the blocks for each of them paragraph, list
  • update each block text using linkyfyjs npm module
  • Finally update and invoke editorjs instance update method and pass the updated blocks to apply the final changes

linkfyjs helps to detect links, metions, tags in a string and html string content and convert that into a final link tag, https://linkify.js.org/

<ReactEditorJS
	tools={Tools}
	defaultValue={data}
	initialData={data}
	data={data}
	onChange={async (e) => {
		const savedData = await e.saver.save();
		const updatedBlocks = savedData.blocks.map((block) => {
			if (block.type === "paragraph") {
				const linkifiedText = linkifyHtml(block.data.text);
				return {
					...block,
					data: { text: linkifiedText },
				};
			}
			return block;
		});

		if (
			JSON.stringify(savedData.blocks) !== JSON.stringify(updatedBlocks)
		) {
			console.log(e?.blocks?.update(updatedBlocks));
		}
	}}
	style={{ background: "white" }}
	onInitialize={(instance) => (editorCore.current = instance)}
/>

shreyvijayvargiya avatar Jul 27 '24 10:07 shreyvijayvargiya