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
![[abc]] ==> without the extension
![[/tuancao/a.md]] ==> Unsupported format
![[/tuancao/a.mp4]]==> Unsupported format
*/
// if (typeof searchText !== "string") { [[picture.jpg]] ==> Link without "!" at the begining won't match
// console.log("The fuck???") ![[abc]] ==> without the extension
// return node ![[/tuancao/a.md]] ==> Unsupported format
// } ![[/tuancao/a.mp4]]==> Unsupported format
*/
const matches = searchText.matchAll(
regex
);
// if (!regex.test(node.value)) return node let startIndex = 0;
let endIndex = searchText.length;
const matches = searchText.matchAll( let children = [];
regex for (const match of matches) {
); endIndex = match.index;
// Constructing text node from un-matched string
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),
};
let startIndex = 0; const imageNode = {
let endIndex = searchText.length; type: "myImage",
url: encodeURI(match[1]), //encode white space from file name
alt: match[1],
};
let children = []; children.push(imageNode);
for (const match of matches) {
endIndex = match.index;
// Constructing text node from un-matched string
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) { if (typeof match[0] !== "string") {
// children.push(textNode); console.log("THE FUCK again")
// } else { }
// console.log("THE FUCK") let matchEndIndex = match.index + match[0].length;
// } startIndex = matchEndIndex;
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;
}; };
} }