kevin940726 / remark-code-import

📝 Populate code blocks from files
https://npm.im/remark-code-import
MIT License
62 stars 11 forks source link

Add Support for line numbers #3

Closed 0phoff closed 3 years ago

0phoff commented 3 years ago

I was wondering whether you were planning support for line numbers as well as filenames ?
If so, I might take a shot at implementing this and sending in a PR, because I would love to have this for a project of mine.

The idea would be to allow for line numbers after the filename:

Code block with single quotes because backticks mess up formatting:
'''js file=./hello.js:5-10
'''

Should separate linenumbers be allowed as well ?
This might not really be useful, but why not (if not too complicated).
What do you think ?
'''js file=./hello.js:1-5;10;13-17
'''

Let me know if you would be interested in such a PR or not!

kevin940726 commented 3 years ago

It does sound interesting!

I'm thinking if this can be done in a different plugin though. Since the whole point of making this a remark plugin is to make it possible to integrate with other plugins in the ecosystem. However, this specific use case does sound very similar to what this plugin does 🤔.

I can't think of any use case where file=./hello.js:1-5;10;13-17 could be useful though. If we want to support this, I wonder what's the best syntax? Maybe just use , as the delimiter?

PR welcome! I'm wondering what's the best choice for the : syntax. IIRC, both Windows and macOS don't support : in the file name, so it should be fine. However, I think Linux supports it. Maybe add an escape hatch \: so that users can explicitly point to the file name instead?

0phoff commented 3 years ago

I'm thinking if this can be done in a different plugin though. Since the whole point of making this a remark plugin is to make it possible to integrate with other plugins in the ecosystem. However, this specific use case does sound very similar to what this plugin does.

This is why I propose to add it to this plugin as opposed to creating a new one. I don't think it would break any existing feature of your plugin and it would add a new one.


I can't think of any use case where file=./hello.js:1-5;10;13-17 could be useful though. If we want to support this, I wonder what's the best syntax? Maybe just use , as the delimiter?

One use-case is showing a class, but maybe skipping the contents of certain (long) methods.


I'm wondering what's the best choice for the : syntax. IIRC, both Windows and macOS don't support : in the file name, so it should be fine. However, I think Linux supports it. Maybe add an escape hatch \: so that users can explicitly point to the file name instead?

I was planning to use the same syntax most services use to point to line numbers, like here in github. You can add line number references when linking to a file by adding :L20-25 etc, and that's the syntax I plan to use (maybe without the L to make it more concise).

I do think you are right colons are officially allowed in linux/unix, but hardly anyone uses it, seeing as a lot of tools use colons to separate paths (eg. PATH variable in your shell). We could check whether the part after the colon matches the line syntax and automatically consider it part of the path otherwise, or we could indeed allow to escape the colon with \:.


I will start working on a PR once I have a bit of spare time! :)

kevin940726 commented 3 years ago

One use-case is showing a class, but maybe skipping the contents of certain (long) methods.

This is probably better handled in a different plugin then. In most cases, we would also want to include line numbers in the rendered output to indicate that we have "skipped" some lines in the code block.

You can add line number references when linking to a file by adding :L20-25 etc, and that's the syntax I plan to use (maybe without the L to make it more concise).

Agreed, looks good to me 👍

We could check whether the part after the colon matches the line syntax and automatically consider it part of the path otherwise, or we could indeed allow to escape the colon with \:

Nvm, I don't think we need to handle escaping it. Matching a pattern sounds like a good enough solution. If the file name does have a colon in the end, then the user should probably rename the file so that it could work in other OSs as well. I agree with your initial recommendation!

I will start working on a PR once I have a bit of spare time! :)

Sure! Take your time :), and thanks a lot! 🤩