py-gfm icon indicating copy to clipboard operation
py-gfm copied to clipboard

Emit names for task list checkboxes

Open ddevault opened this issue 7 years ago • 6 comments

So that, when ticked, we can easily update the text.

ddevault avatar Sep 06 '18 15:09 ddevault

I'm not sure I understand what you mean with "emit".

Checkboxes already have IDs to be used by JavaScripts side processing.

zopieux avatar Sep 08 '18 16:09 zopieux

They do?

>>> import markdown
>>> from mdx_gfm import GithubFlavoredMarkdownExtension
>>> 
>>> source = """
... Hello, *world*! This is a ~~good~~marvelous day!
... Here is an auto link: https://example.org/
... 
... Le me introduce you to [task lists] (https://github.com/blog/1375-task-lists-in-gfm-issues-pulls-comments):
... 
... - [ ] eggs
... - [x] milk
... 
... You can also have fenced code blocks:
... 
... ```
... import this
... ```
... """
>>> md = markdown.Markdown(extensions=[GithubFlavoredMarkdownExtension()])
>>> html = md.convert(source)
>>> html
'<p>Hello, <em>world</em>! This is a <del>good</del>marvelous day!<br />\nHere is an auto link: <a href="https://example.org/">https://example.org/</a></p>\n<p>Le me introduce you to <a href="https://github.com/blog/1375-task-lists-in-gfm-issues-pulls-comments">task lists</a>:</p>\n<ul>\n<li><input disabled="disabled" type="checkbox" /> eggs</li>\n<li><input checked="checked" disabled="disabled" type="checkbox" /> milk</li>\n</ul>\n<p>You can also have fenced code blocks:</p>\n<div class="highlight"><pre><span></span>import this\n</pre></div>'

I would like to do server-side processing, rather than with JavaScript. To that end I'll put the generated Markdown in a form, and if the checkboxes have a name attribute which specifies the character position of the checkbox in the markdown source that generated that checkbox, I can easily run the update on the server.

ddevault avatar Sep 08 '18 16:09 ddevault

For some reason the readthedocs docs are somewhat broken, the list of links to extensions does not show. Please refer to the inline documentation on how to set checkbox_attrs with a function that emits your name. You could use a counter or a hash of the content to generate the name. I'll try to setup an example implementation some day.

zopieux avatar Sep 09 '18 12:09 zopieux

That's not flexible enough, I'd still have to implement my own markdown parser to find the character index of the checkboxes to do the toggle. I just want something like <input type="check" name="check-245"> where 245 is the index of the [ ] in the markdown source.

ddevault avatar Sep 09 '18 14:09 ddevault

I understand your issue, indeed py-gfm does not expose the required API to cleanly toggle the checkbox by modifying the markdown input. It's quite far from the original purpose of a markdown renderer, but I'll try to address this in a new release. You could also come up with a PR that I'll happily review.

zopieux avatar Sep 15 '18 11:09 zopieux

It's quite far from the original purpose of a markdown renderer

I have to disagree. This isn't a traditionally designed parser but it's quite common for a parser to prepare an annotated AST that the consumer can examine, including information about the line number and character index of each semantic object in the source. This is necessary, for example, to show syntax errors with a line and column number. I think it's well within the purview of a markdown implementation.

ddevault avatar Sep 15 '18 15:09 ddevault