fiatjaf / module-linker

browse modules by clicking directly on "import" statements on GitHub
https://module-linker.fiatjaf.com/
MIT License
250 stars 25 forks source link

Manipulate element objects instead of innerHTML #30

Open BehindTheMath opened 6 years ago

BehindTheMath commented 6 years ago

I think it would be more elegant to manipulate the element objects instead of rewriting innerHTML.

For example, this could be substituted with something like this:

var newElem = link.get(0);
var oldTextElem = Array.from(elem.querySelector(".pl-s").childNodes)
    .filter(function (node) { return node.nodeType === 3 })[0];
elem.querySelector(".pl-s").replaceChild(newElem, oldTextElem);
fiatjaf commented 6 years ago

Sorry, but I don't understand. It wouldn't work at all if I replaced that code with this.

The problem is that we can't rely on .pl-s, each language is formatted differently on GitHub. I don't remember now where exactly the problem is, but I hope I'm wrong about it and someday we discover a way to stop using regex and .innerHTML.

BehindTheMath commented 6 years ago

I see what you mean.

I think it could be done, but it would require a separate workflow for each language.

fiatjaf commented 6 years ago

Maybe it is not that hard to implement a separate workflow for each language, and it may be our only choice if the innerHTML replacements continue to generate bugs.

AlexWayfer commented 6 years ago

It can be more difficult and hard to implement, but it's right way for JS, I think.