Browse Source

Markdown: require double tildes for strikethrough

Previously, either double or single tildes would work for strikethrough.
However, this had some strange edge cases, such as attempting to
strikethrough some words that contain a group reference. Since Tildes
uses single tildes for other purposes, it should be much less ambiguous
overall to just force double tildes for strikethrough like this.

This will affect a small number of past posts, in both good and bad
ways (getting rid of some strikethrough, but fixing some accidental
ones).
merge-requests/76/head
Deimos 5 years ago
parent
commit
50d452496d
  1. 21
      tildes/tests/test_markdown.py
  2. 13
      tildes/tildes/lib/cmark.py

21
tildes/tests/test_markdown.py

@ -36,13 +36,30 @@ def test_basic_markdown_unescaped():
def test_strikethrough():
"""Ensure strikethrough works and doesn't turn into a group link."""
markdown = "This ~should not~ should work"
markdown = "This ~~should not~~ should work"
processed = convert_markdown_to_safe_html(markdown)
assert "<del>" in processed
assert "<a" not in processed
def test_no_single_tilde_strikethrough():
"""Ensure using single tildes for strikethrough doesn't work."""
markdown = "This ~will not~ end up as strikethrough."
processed = convert_markdown_to_safe_html(markdown)
assert "<del>" not in processed
def test_strikethrough_with_group():
"""Ensure strikethrough works with a group name in the middle."""
markdown = "They ~~spammed ~music heavily~~ posted lots of songs."
processed = convert_markdown_to_safe_html(markdown)
assert processed.count("<del>") == 1
assert "<a" in processed
def test_table():
"""Ensure table markdown works."""
markdown = (
@ -377,7 +394,7 @@ def test_username_ref_inside_pre_ignored():
def test_group_ref_inside_code_ignored():
"""Ensure a group reference inside a <code> tag doesn't get linked."""
markdown = "Strikethrough works like: `this ~should not~ work`."
markdown = "Link to a group by just writing its name: `see ~news for more`."
processed = convert_markdown_to_safe_html(markdown)
assert "<a" not in processed

13
tildes/tildes/lib/cmark.py

@ -7,11 +7,14 @@ from ctypes import c_char_p, c_int, c_size_t, c_void_p, CDLL
CMARK_DLL = CDLL("/usr/local/lib/libcmark-gfm.so")
CMARK_EXT_DLL = CDLL("/usr/local/lib/libcmark-gfm-extensions.so")
# Enable the --hardbreaks and --unsafe options for cmark
# Can we import these somehow? They're defined in cmark.h as:
# CMARK_OPT_HARDBREAKS (1 << 2)
# CMARK_OPT_UNSAFE (1 << 17)
CMARK_OPTS = (1 << 2) | (1 << 17)
# Enable cmark options: --hardbreaks, --unsafe, --strikethrough-double-tilde
# Can we import these somehow? They're defined in cmark.h with the same values as below
CMARK_OPT_HARDBREAKS = 1 << 2
CMARK_OPT_UNSAFE = 1 << 17
CMARK_OPT_STRIKETHROUGH_DOUBLE_TILDE = 1 << 14
CMARK_OPTS = (
CMARK_OPT_HARDBREAKS | CMARK_OPT_UNSAFE | CMARK_OPT_STRIKETHROUGH_DOUBLE_TILDE
)
CMARK_EXTENSIONS = (b"autolink", b"strikethrough", b"table")

Loading…
Cancel
Save