if (scratch->extensions & EXT_NO_LABELS) {
printf("<h%1d>", temp_short + scratch->base_header_level - 1);
} else {
- temp_char = label_from_token(source, t);
+ temp_token = manual_label_from_header(t, source);
+ if (temp_token) {
+ temp_char = label_from_token(source, temp_token);
+ } else {
+ temp_char = label_from_token(source, t);
+ }
printf("<h%1d id=\"%s\">", temp_short + scratch->base_header_level - 1, temp_char);
free(temp_char);
}
break;
case CODE_FENCE:
case TEXT_EMPTY:
+ case MANUAL_LABEL:
break;
case TEXT_NL:
if (t->next)
}
}
+token * manual_label_from_header(token * h, const char * source) {
+ token * walker = h->child->tail;
+ token * label = NULL;
+ short count = 0;
+
+ while (walker) {
+ switch (walker->type) {
+ case MANUAL_LABEL:
+ // Already identified
+ label = walker;
+ walker = NULL;
+ break;
+ case INDENT_TAB:
+ case INDENT_SPACE:
+ case NON_INDENT_SPACE:
+ case TEXT_NL:
+ case TEXT_LINEBREAK:
+ case TEXT_EMPTY:
+ walker = walker->prev;
+ break;
+ case TEXT_PLAIN:
+ if (walker->len == 1) {
+ if (source[walker->start] == ' ') {
+ walker = walker->prev;
+ break;
+ }
+ }
+ walker = NULL;
+ break;
+ case PAIR_BRACKET:
+ label = walker;
+ while(walker->type == PAIR_BRACKET) {
+ walker = walker->prev;
+ count++;
+ }
+ if (count % 2 == 0) {
+ // Even count
+ label = NULL;
+ } else {
+ // Odd count
+ label->type = MANUAL_LABEL;
+ }
+ default:
+ walker = NULL;
+ }
+ }
+
+ return label;
+}
+
void process_header_to_links(mmd_engine * e, token * h) {
char * label = label_from_token(e->dstr->str, h);
+ // See if we have a manual label
+ token * manual = manual_label_from_header(h, e->dstr->str);
+
+ if (manual) {
+ free(label);
+ label = label_from_token(e->dstr->str, manual);
+ h = manual;
+ }
+
DString * url = d_string_new("#");
d_string_append(url, label);
char * get_fence_language_specifier(token * fence, const char * source);
+token * manual_label_from_header(token * h, const char * source);
+
#endif
--- /dev/null
+<h1 id="bar">foo </h1>
+
+<h1 id="bar">foo</h1>
+
+<h1 id="foobarbat">foo <a href="#bat">bar</a></h1>
+
+<h1 id="bat">foo <a href="#bar">bar</a> </h1>
+
+<h1 id="baz">foo <a href="#bat">bar</a></h1>
+
+<p>5</p>