Skip to content

data-copy for code blocks actually supports newlines #6177

@iBug

Description

@iBug

Description

This part of the documentation is misleading and missing information:

Note that this attribute does not support multiple lines, which is not a limitation of Material for MkDocs, but of the Markdown parser.

In fact, as long as new lines are HTML-encoded (i.e. 
), they stay perfectly intact when copied.

Additionally, if the value to data-copy contains braces, they must be HTML-encoded as well (i.e. { and }), or the Markdown parser breaks on that code block (which I actually suspect to be a bug - they're already inside double quotes).

It would be nice if these tips and quirks are noted in the documentation.

Basic reproduction:

``` { .c data-copy="int main() {
  return 0;
&#125" }
Try copying me for some C code
```

Related links

Proposed change

Remove the offending sentence and add the following text after the original paragraph and example:

Due to restrictions from the Markdown parser, newlines and braces must be
HTML-encoded, e.g. `
` and `{`. For example:

```` markdown title="Code block"
``` { .c data-copy="int main() {
  return 0;
&#125" }
Try copying me for some C code
```
````

<div class="result" markdown>

``` { .c data-copy="int main() &#123;&#10;  return 0;&#10;&#125" }
Try copying me for some C code
```

</div>

I couldn't work out a satisfactory replacement by myself, so this is just a suggestion. A better example is certainly welcome.

Before submitting

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationIssue concerns the documentation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions