r/javascript • u/Fighter178 • Apr 06 '23
New Library for creating HTML with Javascript
https://github.com/Fighter178/micromarkup4
u/cocosin Apr 06 '23
I made almost the same thing for a pure js widget to inject on external sites in my latest project. Thanks for the lib, didn’t find anything like that in my latest research
A few of my comments on the code review:
Why do you create a div as a wrapper? You can create a fragment and return it. Now it adds additional unpredictable layer to the markup
You don’t have additional argument in the addEventListener function, so there’s no chance to add logic for the bubbling/capturing functionality
the mk and n functions look the same. You can use a main helper for this to reduce the size
In my product I needed to add raw html as a child when creating
2
u/Fighter178 Apr 06 '23 edited Apr 06 '23
I'm gonna update it. Also, I did intend to add code for the event func, but I forgot to add it. And yes, I should just return a document fragment. I'll probably update it. Also, if you need raw html, you can access the innerHTML of the element.
1
2
u/lookitsatoucan Apr 06 '23
Nice job! What problems is this library designed to solve?
2
u/Fighter178 Apr 06 '23
I needed to make a bunch of elements in pure js for a project, and I just made this to make it easier and faster to write.
1
u/Remedynn Apr 06 '23
How do you manage state? Or do the user just need to use document getters?
1
u/Fighter178 Apr 06 '23
I might add that, but if you need a library that makes HTML with state, check out another library I made, firefly.js
1
Apr 06 '23
I see some potential to use this, I think it's gonna be helpful for me, especially with adding event listeners. One thing - can you add a functionality where I could change "mk" function HTMLElement wrapper from div to something else, like in "node" function?
1
u/Fighter178 Apr 06 '23
Sure, I might make it return a document fragment, or possibly allow you to change it.
1
Apr 06 '23
Not to shit on op's post but i would myself rather use something like lit-html instead
0
u/Fighter178 Apr 06 '23
That's true, though for the original project I made this for, I had to use pure js, and now it can be useful for more people.
0
u/Fighter178 Apr 06 '23
Its also available on NPM: here
I know the docs are bad, you can fix them if you want.
2
u/Fighter178 Apr 06 '23
And will work with a CDN, with this: https://cdn.jsdelivr.net/gh/fighter178/micromarkup@latest/index.min.js Or : https://cdn.jsdelivr.net/npm/micromarkup
1
-3
u/jeremrx Apr 06 '23
1
u/Fighter178 Jun 28 '23
Its meant to be fast to write. Imagine trying to write out "element" each time you need one. (Yes, I'm intelisense helps)
1
20
u/[deleted] Apr 06 '23
Why?