Comments (8)
Hi @EmilePerron, thanks so much for reaching out - I think it's a great feature and I'd love a PR! π
On first pass, this may not be a trivial feature to implement. Some thoughts come to mind:
-
Probably both
<script>
and style<template>
tags should be observed for changes. -
I guess mutation callbacks must be debounced so multiple calls will only trigger a re-render once. (I didn't read in detail on
MutationObserver
specs though, maybe it's native?) -
Perhaps the
render()
function can be re-factored to support conditional DOM stamping (or re-building)? Something like ifrender()
is called twice in a row, checks are added so that the second call shouldn't actually re-stamp i.e.render()
will only render changes.
There are probably other optimisations and gotchas too, so let's keep this thread open for discussion! Also do let me know if there's anything I can help with. Thanks so much for you time once again. π
from zero-md.
@zerodevx Glad to see I'm not the only one interested in this!
Here are some of my thoughts after reading your comment and doing some exploration of the code:
- Agreed - both styles and content could be observed and trigger partial re-renders.
- The
MutationObserver
will only trigger once in situations where multiple changes occur one after another, unless there are timeouts, delays or painting in between changes. So I don't think debouncing will be necessary. (as other changes often take place around the component at the same time, even a 1ms debounce makes the component seem slightly sluggish compared to non-debounced content and elements around it). - Perhaps
render()
could be refactored to handle both the full rendering and the "partial" rendering, but that would mean the method has multiple purposes, and would do different things in subsequent calls. This might be more confusing to users and developers alike. It could also break existing codebases that manually callrender()
. Making new methods to support the partial rendering/replacement of styles and content seems wiser.
Those are my early thoughts - once again, feel free to chime in if you disagree or have anything else to add!
I'll keep working on a PR in my free time; a PR will come as soon as I've got something tangible to share π
from zero-md.
All sounds good so far! Looking forward to this - thanks for taking the time! π
from zero-md.
Just opened a WIP PR for this here: #51 . Here's the gist of it:
- Insertion and removal of
<template>
and<script type="text/markdown">
tags in the component are detected with aMutationObserver
and trigger a partial re-rendering of styles and/or content (via the newrefreshStyle()
and/orrefreshContent()
) - Updates to
<template>
and<script type="text/markdown">
elements in the component also trigger partial re-rendering of the appropriate part of the component, using the same methods. - When a partial re-rendering occurs, the
zero-md-rendered
event is fired just like in therender()
method. However, it includes apartial
flag and apart
indicator in theevent.detail
(ex.:this.fire('zero-md-rendered', { partial: true, part: 'body' })
) - This feature is inactive when the component is in
manual-render
Β mode.
I have included simple test cases for both styles and content changes, which are both passing at the moment.
More tests could likely be added to ensure there's no regression in relation with other features (ex.: no-shadow
, manual-render
, more complex manipulations, etc.).
You'll likely want to review the code style - I tried to match yours as closely as I could (and lint checks all pass), but I tend to be very verbose and space heavy, so it might not be up to your standards π¬
Let me know if you have any feedback.
Cheers!
from zero-md.
Wow that's super quick! π Really appreciate your contribution! Do give me some time to review/test in detail - I'll update you as soon as I can. Thanks once again!
from zero-md.
Hello @zerodevx π
Have you had some time to look into the #51?
If not, would it be possible to get a [very approximative] ETA?
I have a workaround that works just fine for development purposes in my project, so there's no real rush - just curious π€
Cheers!
from zero-md.
Hi @EmilePerron thanks for your note - my apologies! I'll try to clear my open-source debts this weekend! π
from zero-md.
Hi @EmilePerron
My apologies for the wait. I (finally) reviewed PR #51 and it generally looks very good.
I have included simple test cases for both styles and content changes, which are both passing at the moment.
Thanks for adding the tests. They look very good!
Insertion and removal of
<template>
and<script type="text/markdown">
tags in the component are detected with a MutationObserver and trigger a partial re-rendering of styles and/or content (via the new refreshStyle() and/or refreshContent())
Updates to
<template>
and<script type="text/markdown">
elements in the component also trigger partial re-rendering of the appropriate part of the component, using the same methods.
I still feel there's an opportunity to refactor the render()
method to render changes instead, which removes the need for refreshStyle()
and refreshContent()
- and IMO simplifies the codebase as well.
When a partial re-rendering occurs, the zero-md-rendered event is fired just like in the render() method. However, it includes a partial flag and a part indicator in the event.detail (ex.: this.fire('zero-md-rendered', { partial: true, part: 'body' }))
Thanks for the recommendation. I'll document this API change.
This feature is inactive when the component is in manual-render mode.
Good catch!
More tests could likely be added to ensure there's no regression in relation with other features (ex.: no-shadow, manual-render, more complex manipulations, etc.).
Coverage seems OK so far; we can add as and when bug reports surface. π
You'll likely want to review the code style - I tried to match yours as closely as I could (and lint checks all pass), but I tend to be very verbose and space heavy, so it might not be up to your standards π¬
Thanks for doing this! I don't personally have big opinions on this - as long as it passes standardjs
linting it's an OK.
I'll probably merge this upstream in a bit - I'll also see if there're opportunities to simplify, if not today then soon. Thanks so much!
from zero-md.
Related Issues (20)
- Thanks a ton!
- Parsing img tags in md [ERROR with src] HOT 1
- fallback markdown script renders as code block HOT 1
- Marked.js v5 support HOT 2
- github markdown emoji HOT 3
- How to use zeroMd as module? HOT 3
- Cyrillic anchor links doesn't works HOT 2
- nginx example HOT 3
- BUG: `goto` (scroll to selected element) broken when `no-shadow` enabled HOT 2
- Discussion: Anchor.js usability (for on-hover anchor links) HOT 5
- Mathjax zero-md@{1,2} bug HOT 3
- Mathjax no-shadow window.ZeroMdConfig HOT 2
- Render Mathjax on content change HOT 2
- Is there a way to use mathjax while still supporting shadow dom? HOT 2
- Does zero-md support admonition marked plug-in? HOT 7
- Parse/Render text within HTML tags HOT 2
- prism.js seems to not recognize C++? HOT 3
- Mermaid Support Broken Since Version 2.5.1 HOT 3
- Display table of content TOC HOT 4
- Markdown not translated into HTML HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zero-md.