editor.js
editor.js copied to clipboard
Feature: auto link
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
i hope so too
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)}
/>