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,5 +1,7 @@
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;
@ -25,19 +27,10 @@ function convertTextNode(node) {
![[/tuancao/a.md]] ==> Unsupported format ![[/tuancao/a.md]] ==> Unsupported format
![[/tuancao/a.mp4]]==> Unsupported format ![[/tuancao/a.mp4]]==> Unsupported format
*/ */
// if (typeof searchText !== "string") {
// console.log("The fuck???")
// return node
// }
// if (!regex.test(node.value)) return node
const matches = searchText.matchAll( const matches = searchText.matchAll(
regex regex
); );
let startIndex = 0; let startIndex = 0;
let endIndex = searchText.length; let endIndex = searchText.length;
@ -52,12 +45,6 @@ function convertTextNode(node) {
value: searchText.substring(startIndex, endIndex), value: searchText.substring(startIndex, endIndex),
}; };
// if (typeof textNode.value ==='string' && textNode.value.length > 0) {
// children.push(textNode);
// } else {
// console.log("THE FUCK")
// }
const imageNode = { const imageNode = {
type: "myImage", type: "myImage",
url: encodeURI(match[1]), //encode white space from file name url: encodeURI(match[1]), //encode white space from file name
@ -90,9 +77,11 @@ function convertTextNode(node) {
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) => {
if (regex.test(node.value)) {
const newNode = convertTextNode(node); const newNode = convertTextNode(node);
node.type = "paragraph"; node.type = "paragraph";
node.children = newNode.children; node.children = newNode.children;
}
}); });
// Change back "text-temp" node ==> "text" to clean up // Change back "text-temp" node ==> "text" to clean up