-
-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: support using unified as a template tag. #40
feature: support using unified as a template tag. #40
Conversation
0bfa080
to
4527d01
Compare
This comment has been minimized.
This comment has been minimized.
Wow, this is super interesting! I’m mainly wondering though, why do this? As in, your first example could be rewritten like so: var unified = require('unified')
var markdown = require('remark-parse')
var remark2rehype = require('remark-rehype')
var doc = require('rehype-document')
var format = require('rehype-format')
var html = require('rehype-stringify')
var html = unified()
.use(markdown)
.use(remark2rehype)
.use(doc)
.use(format)
.use(html)
.processSync
console.log(html(`# Hello world!`).toString()) Note the |
Template tags have a different signature than process expects. console.log`1 ${2} 3 ${4}`
// [ "1 ", " 3 ", "" ]
// 2
// 4 Templates will take the static text and turn it into an array. This new function applies the console.log(String.raw`1 ${2} 3 ${4}`)
// '1 2 3 4' |
Right I understand that part, but for your example, one could also do I get that it’s new and interesting, and maybe that’s good enough, but I don’t see much added value over calling a function ;) |
As it stands it's mostly syntax sugar to avoid needing Where it could go is more interesting, similar to how MDX allows adding non-markdown content mid-document, supporting template literals opens up the possibility of being able to mixin new content types mid-document into any unified supported language. |
@ChristianMurphy Right! So it gets interesting when it would be possible to “evaluate” the tag and the template in a build step? |
My thought would be to use data type of interpolated values to determine how to evaluate. |
In the alternative track, unified would parse the content surrounding the interpolated value, then add the interpolated value directly into the AST. E.G. var unified = require('unified')
var markdown = require('remark-parse')
var remark2rehype = require('remark-rehype')
var doc = require('rehype-document')
var format = require('rehype-format')
var html = require('rehype-stringify')
const h = require('hastscript')
var md2ht = unified()
.use(markdown)
.use(remark2rehype)
.use(doc)
.use(format)
.use(html)
.templateTagSync
md2ht`## ${h('samp', 'sample header')}` would generate <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h2>
<samp>
sample header
</samp>
</h2>
</body>
</html> |
@ChristianMurphy Huhh, very interesting indeed! I like where this is heading. But I foresee that a) this feature would change in the future, as b) we’re not entirely sure how this would work yet. It could of course be that first we need the implementation before we can create interesting cases, but I feel like it’s better to develop both together. Develop both the implementation and the use-cases together? |
I agree. |
@Hypercubed interested in hearing your thoughts on this! |
It's very interesting when you add the value interpolation, as you discussed. In
All this said I think it would be very interesting if unified was able to convert to something like |
Thanks for the feedback @wooorm and @Hypercubed!
It would be. |
Inspired by hyperHTML and lit-html.
This PR adds new
templateTag
andtemplateTagSync
functions.These new functions allow unified to be applied as a tag/modifier function to any template string.
Simple Example:
Yields:
Interpolation Example:
Yields: