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 16:51:00 +07:00
parent 4dc41ad032
commit 6a144536e6

View File

@ -1,105 +1,94 @@
import { visit } from "unist-util-visit"; import {visit} from "unist-util-visit";
const regex = /\!\[\[(([a-z\-_0-9\\/\:]+\s*)+\.(jpg|jpeg|png|gif|svg|webp))]]/gi; const regex = /\!\[\[(([a-z\-_0-9\\/\:]+\s*)+\.(jpg|jpeg|png|gif|svg|webp))]]/gi;
function convertTextNode(node) { function convertTextNode(node) {
const searchText = node.value; const searchText = node.value;
/* /*
This regex MATCH following type of image link This regex MATCH following type of image link
![[youtube.png]] ==> Image with no folder path, no space in between ![[youtube.png]] ==> Image with no folder path, no space in between
![[a .png]] ==> with space ![[a .png]] ==> with space
![[tuancao/a.png]] ==> file path WITHOUT space ![[tuancao/a.png]] ==> file path WITHOUT space
![[tuancao/a .png]] ==> file path WITH space ![[tuancao/a .png]] ==> file path WITH space
Image with extension: jpg, jpeg, gif, svg, webp, png Image with extension: jpg, jpeg, gif, svg, webp, png
![[/tuancao/a.jpg]] ![[/tuancao/a.jpg]]
![[/tuancao/a.webp]] ![[/tuancao/a.webp]]
![[/tuancao/a.png]] ![[/tuancao/a.png]]
![[/tuancao/a.jpeg]] ![[/tuancao/a.jpeg]]
... ...
--- ---
Will NOT match following case Will NOT match following case
[[picture.jpg]] ==> Link without "!" at the begining won't match [[picture.jpg]] ==> Link without "!" at the begining won't match
![[abc]] ==> without the extension ![[abc]] ==> without the extension
![[/tuancao/a.md]] ==> Unsupported format ![[/tuancao/a.md]] ==> Unsupported format
![[/tuancao/a.mp4]]==> Unsupported format ![[/tuancao/a.mp4]]==> Unsupported format
*/ */
const matches = searchText.matchAll(
regex
);
// if (typeof searchText !== "string") { let startIndex = 0;
// console.log("The fuck???") let endIndex = searchText.length;
// return node
// }
// if (!regex.test(node.value)) return node let children = [];
for (const match of matches) {
endIndex = match.index;
const matches = searchText.matchAll( // Constructing text node from un-matched string
regex const textNode = {
); // change type child node, so that visit() function won't recursively visit this node with "text" type
type: "text-temp",
value: searchText.substring(startIndex, endIndex),
};
const imageNode = {
type: "myImage",
url: encodeURI(match[1]), //encode white space from file name
alt: match[1],
};
let startIndex = 0; children.push(imageNode);
let endIndex = searchText.length;
let children = []; if (typeof match[0] !== "string") {
for (const match of matches) { console.log("THE FUCK again")
endIndex = match.index; }
let matchEndIndex = match.index + match[0].length;
// Constructing text node from un-matched string startIndex = matchEndIndex;
const textNode = {
// change type child node, so that visit() function won't recursively visit this node with "text" type
type: "text-temp",
value: searchText.substring(startIndex, endIndex),
};
// if (typeof textNode.value ==='string' && textNode.value.length > 0) {
// children.push(textNode);
// } else {
// console.log("THE FUCK")
// }
const imageNode = {
type: "myImage",
url: encodeURI(match[1]), //encode white space from file name
alt: match[1],
};
children.push(imageNode);
if (typeof match[0] !== "string") {
console.log("THE FUCK again")
} }
let matchEndIndex = match.index + match[0].length;
startIndex = matchEndIndex;
}
if (startIndex < searchText.length) { if (startIndex < searchText.length) {
const textNode = { const textNode = {
type: "text-temp", type: "text-temp",
value: searchText.substring(startIndex, searchText.length), value: searchText.substring(startIndex, searchText.length),
};
children.push(textNode);
}
return {
type: "paragraph",
children: children,
}; };
children.push(textNode);
}
return {
type: "paragraph",
children: children,
};
} }
export default function attacher(options) { export default function attacher(options) {
return function transformer(tree, vfile) { return function transformer(tree, vfile) {
visit(tree, "text", (node) => { visit(tree, "text", (node) => {
const newNode = convertTextNode(node); if (regex.test(node.value)) {
node.type = "paragraph"; const newNode = convertTextNode(node);
node.children = newNode.children; node.type = "paragraph";
}); node.children = newNode.children;
}
});
// Change back "text-temp" node ==> "text" to clean up // Change back "text-temp" node ==> "text" to clean up
visit(tree, "text-temp", (node) => { visit(tree, "text-temp", (node) => {
node.type = "text"; node.type = "text";
}); });
return tree; return tree;
}; };
} }