zopieux / py-gfm

Github-Flavored Markdown for Python-Markdown.
https://pypi.org/project/py-gfm/
BSD 3-Clause "New" or "Revised" License
71 stars 14 forks source link

Emit names for task list checkboxes #12

Open ddevault opened 6 years ago

ddevault commented 6 years ago

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

zopieux commented 6 years ago

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

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

ddevault commented 6 years ago

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.

zopieux commented 6 years ago

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.

ddevault commented 6 years ago

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.

zopieux commented 6 years ago

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.

ddevault commented 6 years ago

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.