cmark

My personal build of CMark ✏️

Commit
4b21fe65b6f774193ce25b6f0ebee5cf88b88200
Parent
0bf07dda1cbbdaf0ae7696be1f0c37169689539f
Author
John MacFarlane <jgm@berkeley.edu>
Date

Commonmark renderer: improved escaping.

Diffstat

1 file changed, 10 insertions, 6 deletions

Status File Name N° Changes Insertions Deletions
Modified src/commonmark.c 16 10 6
diff --git a/src/commonmark.c b/src/commonmark.c
@@ -36,14 +36,17 @@ static inline void blankline(struct render_state *state)
 	}
 }
 
-static inline bool needs_escaping(int32_t c, unsigned char d)
+static inline bool
+needs_escaping(int32_t c, unsigned char next_c, struct render_state *state)
 {
-	// TODO escape potential list markers at beginning of line
-	// (add param)
 	return (c == '*' || c == '_' || c == '[' || c == ']' ||
 		c == '<' || c == '>' || c == '\\' ||
-		(c == '&' && isalpha(d)) ||
-		(c == '!' && d == '['));
+		(c == '&' && isalpha(next_c)) ||
+		(c == '!' && next_c == '[') ||
+		(state->begin_line &&
+		 (c == '-' || c == '+' || c == '#' || c == '=')) ||
+		((c == '.' || c == ')') &&
+		 isdigit(state->buffer->ptr[state->buffer->size - 1])));
 }
 
 static inline void out(struct render_state *state,
@@ -103,7 +106,8 @@ static inline void out(struct render_state *state,
 			state->column = 0;
 			state->begin_line = true;
 			state->last_breakable = 0;
-		} else if (escape && needs_escaping(c, nextc)) {
+		} else if (escape &&
+			   needs_escaping(c, nextc, state)) {
 			cmark_strbuf_putc(state->buffer, '\\');
 			utf8proc_encode_char(c, state->buffer);
 			state->column += 2;