Fixed messed up layout because of over transforming text node that don't match search pattern.

This commit is contained in:
Tuan Cao 2022-04-28 17:15:28 +07:00
parent 6a144536e6
commit c8ab797a15

View File

@ -1,6 +1,7 @@
import {visit} from "unist-util-visit";
const regex = /\!\[\[(([a-z\-_0-9\\/\:]+\s*)+\.(jpg|jpeg|png|gif|svg|webp))]]/gi;
const regex2 = /\!\[\[(([a-z\-_0-9\\/\:]+\s*)+\.(jpg|jpeg|png|gif|svg|webp))]]/gi; //TODO why can't I reuse regex literal???
function convertTextNode(node) {
const searchText = node.value;
@ -46,7 +47,8 @@ function convertTextNode(node) {
};
const imageNode = {
type: "myImage",
type: "image",
//TODO: Use some kind of option to pass in default images path
url: encodeURI(match[1]), //encode white space from file name
alt: match[1],
};
@ -77,7 +79,7 @@ function convertTextNode(node) {
export default function attacher(options) {
return function transformer(tree, vfile) {
visit(tree, "text", (node) => {
if (regex.test(node.value)) {
if (regex2.test(node.value)) {
const newNode = convertTextNode(node);
node.type = "paragraph";
node.children = newNode.children;