pymdown-extensions icon indicating copy to clipboard operation
pymdown-extensions copied to clipboard

Improvements to snippets with multiple lines from the same file

Open shaedrich opened this issue 1 year ago • 6 comments
trafficstars

Description

Let's assume, we have a snippet, where we want to "collapse" the middle part

// we want this line
    // we don't want this line
    // we want this line

Currently, this can only be done with the block format

```
--8<--
my_file:1:1
my_file:3:3
--8<--
```

Benefits

It works but it could be improved

  • It would be just handy if we could write something like this
--8<-- "my_file:1,3"

or even something like

--8<-- "my_other_file:1:2,4:5"

Solution Idea

  • But where it really would be without any alternative, is when using with auto dedention (#1945). Line 3 would always be incorrectly be dedented. This could, if the previous solution is not wanted, be done by taking all snippets from the same file into account but I can see that this is not a very

shaedrich avatar May 26 '24 04:05 shaedrich

I am not certain if we will go in this direction. We are certainly not prevented from specifying multiple lines as it can be done in block format. With that said, I will not rule this out as a possibility at this time for the future.

facelessuser avatar May 27 '24 13:05 facelessuser

I never said, we were prevented of specifying lines. As I said, it can even lead to problems with dedention.

shaedrich avatar May 27 '24 13:05 shaedrich

I never claimed you said it was not possible, I was just reiterating that this does not enable something that could not previously be done. This enables no new functionality per se, but is an enhancement to syntax. No one is blocked due this request not being fulfilled. Multiple lines are possible in the current framework. For this reason, it is likely less critical and would be lower priority. But I've left the idea open because I can see that it may be viewed as less cumbersome to some, assuming there are no conflicts with the syntax.

I will sometimes restate things so I can reference my comment without having to read the whole thread. It helps me remember my thinking at the time.

facelessuser avatar May 27 '24 13:05 facelessuser

I will sometimes restate things so I can reference my comment without having to read the whole thread. It helps me remember my thinking at the time.

Thanks for the explanation :+1:

This enables no new functionality per se, but is an enhancement to syntax.

Since it's your repository, you are free to change the label from "T: feature" to "T: enhancement" or whatever you like. I would have chosen that myself, but it wasn't an option when creating the issue and I didn't want to submit it as a bug report.

One way to solve the presented problem would come from #2217 if you happen to stumble upon a solution that works for you there. #2386 might present an alternative.

shaedrich avatar May 27 '24 13:05 shaedrich

After giving this some thought, I'm okay with extending this. I'll probably implement something like this in the next release.

facelessuser avatar Sep 30 '24 16:09 facelessuser

This didn't make the cut for the next release, but it should make it in the one after.

facelessuser avatar Oct 23 '24 13:10 facelessuser

Hi, I stumbled upon this discussion when I encountered an issue with specifying sections as documented in "Snippet Sections" (https://facelessuser.github.io/pymdown-extensions/extensions/snippets/#snippet-sections).

It seems to work, the marked section is included properly. However, the --8<-- [start:name] and --8<-- [end:name] lines are shown in the output of the file where they are defined:

### On Linux

--8<-- [start:include-linux-solr-update]

!!! warning "Preparation if service was installed with init.d"
    If you installed the Search Server via init.d, you need to delete the init.d service unit before you can update the service. Execute the following commands:
    
    ```java
    sudo chkconfig --del celum-searchserver
    sudo rm /etc/init.d/celum-searchserver
    ```

1.  Navigate to the download location of your old Solr™ version's distribution package (.tgz file) and **delete** it.
2. ...
3. ...
...

--8<-- [end:include-linux-solr-update]

In the output, the lines are visible:

image

We are using Material for Mkdocs as theme.

In light of your last comment (3 weeks ago), I wonder if maybe it isn't actually supposed to work yet?

How can I check which version of pymdown-extensions.snippets our project is using?

Thank you :)

bimCelum avatar Nov 13 '24 15:11 bimCelum

@bimCelum, please open up a separate issue for your problem so we can triage it. We should not be using specific issues as a dumping ground for any issue that happens to be related to Snippets. I'm happy to look at the issue if you can do so.

facelessuser avatar Nov 13 '24 16:11 facelessuser

@bimCelum also, if you are doing this in a MkDocs Material environment, please create a minimal, reproducible example following their guide: https://squidfunk.github.io/mkdocs-material/guides/creating-a-reproduction/?h=minimal#minimal-reproduction. This helps save me time from having to reconstruct a working example to make sure there is nothing wrong in your environment that you are overlooking.

facelessuser avatar Nov 13 '24 16:11 facelessuser

@shaedrich

A PR is up for multi-line selections.

Keep in mind that each line selection is evaluated independently of previous selections. Selections will be performed in the order they are specified. No additional separators (empty lines or otherwise) are inserted between selections, they are inserted exactly as specified.

facelessuser avatar Dec 22 '24 16:12 facelessuser

Thanks a lot for implementing it and giving me a heads-up 👍🏻 I'm excited for this :) Left you a review—hope, it's helpful 😉

shaedrich avatar Dec 22 '24 16:12 shaedrich