commit ad4fccd

grunfink  ·  2023-07-24 10:49:18 +0000 UTC
parents a940d14, ae1f0ac
Merge pull request 'Allow several tags & fix video element width, and more.' (#74) from yonle/snac2:master into master

Reviewed-on: https://codeberg.org/grunfink/snac2/pulls/74
2 files changed,  +27, -8
+3, -2
1@@ -1,5 +1,6 @@
2-body { max-width: 48em; margin: auto; line-height: 1.5; padding: 0.8em }
3-img { max-width: 100% }
4+body { max-width: 48em; margin: auto; line-height: 1.5; padding: 0.8em; word-wrap: break-word; }
5+pre { overflow-x: scroll; }
6+.snac-embedded-video, img { max-width: 100% }
7 .snac-origin { font-size: 85% }
8 .snac-score { float: right; font-size: 85% }
9 .snac-top-user { text-align: center; padding-bottom: 2em }
+24, -6
 1@@ -56,7 +56,8 @@ static xs_str *format_line(const char *line, xs_list **attach)
 2             /* markup */
 3             if (xs_startswith(v, "`")) {
 4                 xs *s1 = xs_crop_i(xs_dup(v), 1, -1);
 5-                xs *s2 = xs_fmt("<code>%s</code>", s1);
 6+                xs *e1 = encode_html(s1);
 7+                xs *s2 = xs_fmt("<code>%s</code>", e1);
 8                 s = xs_str_cat(s, s2);
 9             }
10             else
11@@ -134,11 +135,28 @@ xs_str *not_really_markdown(const char *content, xs_list **attach)
12             continue;
13         }
14 
15-        if (in_pre)
16-            ss = xs_dup(v);
17+        if (in_pre) {
18+            // Encode all HTML characters when we're in pre element until we are out.
19+            ss = encode_html(xs_dup(v));
20+
21+            s = xs_str_cat(s, ss);
22+            s = xs_str_cat(s, "<br>");
23+            continue;
24+        }
25+
26         else
27             ss = xs_strip_i(format_line(v, attach));
28 
29+        if (xs_startswith(ss, "---")) {
30+            /* delete the --- */
31+            ss = xs_strip_i(xs_crop_i(ss, 3, 0));
32+            s = xs_str_cat(s, "<hr>");
33+
34+            s = xs_str_cat(s, ss);
35+
36+            continue;
37+        }
38+
39         if (xs_startswith(ss, ">")) {
40             /* delete the > and subsequent spaces */
41             ss = xs_strip_i(xs_crop_i(ss, 1, 0));
42@@ -186,8 +204,8 @@ xs_str *not_really_markdown(const char *content, xs_list **attach)
43 
44 
45 const char *valid_tags[] = {
46-    "a", "p", "br", "br/", "blockquote", "ul", "ol", "li", "cite",
47-    "span", "i", "b", "u", "pre", "code", "em", "strong", NULL
48+    "a", "p", "br", "br/", "blockquote", "ul", "ol", "li", "cite", "small",
49+    "span", "i", "b", "u", "pre", "code", "em", "strong", "hr", "img", "del", NULL
50 };
51 
52 xs_str *sanitize(const char *content)
53@@ -219,7 +237,7 @@ xs_str *sanitize(const char *content)
54 
55             if (valid_tags[i]) {
56                 /* accepted tag: rebuild it with only the accepted elements */
57-                xs *el = xs_regex_match(v, "(href|rel|class|target)=\"[^\"]*\"");
58+                xs *el = xs_regex_match(v, "(src|href|rel|class|target)=\"[^\"]*\"");
59                 xs *s3 = xs_join(el, " ");
60 
61                 s2 = xs_fmt("<%s%s%s%s>",