Closed jknoxville closed 3 years ago
Thank you for opening this!
This feature does sound interesting, but I don't think that I would like this to be implemented in this plugin. The main consideration is that this plugin is meant to be simple. I want this plugin to be simple to use, with minimal magic and features, and as little configuration as possible. I would imagine a feature like this to be implemented as a different remark plugin. We should be able leverage the plugin system of remark
and use them together. Use remark-code-import
to copy the content of the file to the code block, and use a different plugin (maybe like remark-trim-code-block
?) to trim the content of the code block. Not only does this make this plugin simple but also make it possible for the users to combine different plugins to achieve more complex situations. Does it make sense :)?
The reason why I think #3 can be included in this plugin is that it is indeed a simple enough use case, and I also don't want any more features beyond that. To be honest, I'm one the edge of that feature either, but I'm more lean on allowing that for now.
Another consideration of your proposal is that this plugin should allow any language file to be imported to the code block. //
is a common comment syntax for many languages, but might be considered syntax error in the others. Not to mention that it doesn't make much sense in plain text files either. Requiring the use of a predefined syntax in the code block means that the files to be imported is "part of" this plugin, but not just any files in your file system. (For instance, sometimes we might want to run the file we're trying to import.)
Gotcha, that makes sense. Thanks for the response.
This would make things a lot easier for me right now. I'm porting a site from Ornate, which supports this.
There shouldn't be different start and end markers, they should be paired in the source file. You could have an open/close marker syntax, but Ornate simply uses the same tag in pairs, like this:
//#features-composable
// Create a query for coffee names with a price less than 10, sorted by name
coffees.filter(_.price < 10.0).sortBy(_.name).map(_.name)
// The generated SQL is equivalent to:
// select name from COFFEES where PRICE < 10.0 order by NAME
//#features-composable
I think if it occurs multiple times, all pairs are included (that is, when you hit the first occurrence, you start including lines, when you hit the 2nd, you start omitting lines, after the 3rd you include lines again, and so on). This way you can use one 'label' to pick multiple fragments. Anyway that's how Ornate works...
Anyway besides that it would be less porting work, using named regions is hugely beneficial for the simple reason that code changes over time. With line number ranges, every time the file is changed you have to remember to update the line number references, and even if you do remember, doing so can be time consuming. Named ranges are much more maintenance-friendly.
@nafg As stated above, I don't think such functionality should be implemented in this plugin. Feel free to create another plugin and chain it with remark-code-import
. Something like (not tested)...
function stripContentOnMarker({ marker = '#MARKER#' } = {}) {
return (tree) => {
visit(tree, 'code', node => {
const lines = node.value.split(new RegExp(`\n${marker}\n?`));
// No markers found
if (lines.length === 1) {
return;
}
// Join lines in between markers and remove others
node.value = lines.filter((_, i) => i % 2 === 1).join('\n');
});
}
}
// Use it like
remark()
// We have to first import the code that it's in the code block to be processed
.use(remarkCodeImport)
// Then we can run your custom plugin to strip the content
.use(stripContentOnMarker)
.process(yourFile)
Feature
Here's what I am envisioning:
Mardown usage:
gets tranformed into: