Feature: download button for code blocks#32
Conversation
|
I've set version |
|
Here are a few suggestions:
|
@jaywhj |
|
(1) (2) (3)
(4) Most parsers should implement a mapping between language types and file extensions; we need to look up how to use that mapping. |
|
I've made some changes based on your feedback. (1) It is not possible to set
I've thought of (2) Fallback filename and language extension The language extension detection is already implemented: the language-* class Pygments adds to the container
A config option for the fallback name can be added later if needed. If we want to map languages to extensions, we would need a dictionary for storing the transformations. The only question is how exhaustive you (3) Attribute name We went with (4) Filename priority and processing Agreed — filename should be used as-is with no processing. Title similarly. The priority chain is now:
|
|
I just ran some tests, and there are quite a few issues with this download feature. All of the following cases failed to parse: Actual supported usage: Furthermore, even this format fails to parse, meaning no other attributes can be added after the language type: Under these circumstances, users will definitely not accept this, because the original default behavior already supports parsing multiple attributes Can we remove the curly brace syntax? Let’s just add the new attributes directly to the existing format, for example: Additionally, this configuration option currently has no effect:
|
Suggestions for Download Type DetectionThe recommended priority order is as follows: (1) Check if (2) Check if (3) Check if (4) If none of the above, treat it as an invalid download. Suggestions for Download FilenamesI think the With this adjustment, the overall design and usage will become much simpler. |
|
Hi, I’ve redesigned the download feature over the past few days.
Feel free to test it: Enable toggle: markdown_extensions:
- material.extensions.code_downloadThe following formats are all supported: While this implements the desired functionality and offers a more convenient writing experience for users, I feel the added complexity is hardly worthwhile. In fact, direct HTTP links written natively in Markdown already support downloads out of the box, this enhancement only improves the user experience specifically for blob content. To support the raw syntax for |
|
I've further optimized the code and structure. Feel free to test the latest commit: Alternatively, you can directly checkout the Two usage methods are now supported:
The upstream discussion is here: facelessuser/pymdown-extensions#2885 |
This PR adds a download button to the code blocks following this syntax:
data-download="1": Download the code block content. The filename suggested to the browser is resolved in the following order of priority:data-download-filenameattribute on the code blocktitleoption on the code blockdata-download=url: Create a link button with a download attribute. The filename suggested to the browser is resolved in the following order of priority:data-download-filenameattribute on the code blocktitleoption on the code blockCode block buttons now handle translations properly (
code.selecttranslation was missing).English and Catalan translations are updated with this changes.