diff --git a/issues.md b/issues.md deleted file mode 100644 index 90c4170..0000000 --- a/issues.md +++ /dev/null @@ -1,59 +0,0 @@ -# Issues - -## TODO - -### LaTeX rendering - -Please explain how LaTeX rendering is achieved and what postprocessing is performed on the returned GitHub and GitLab HTML to make it work with the MathJax library. - -Then determine whether cmark differs from GitHub. - -### Different conversion for different converters - -We need an architecture (or at least a coding pattern) to handle converting Markdown to HTML for different converters. - -The basic Markdown-to-HTML conversion is `cmarkmd2html` or `apimd2html(..., service)`. There is plenty in common between GitHub and GitLab, and the differences are parameterized using the config JSON files. - -Then convert the HTML to XML. This mainly involves self-closing tags such as `br` and `img` so the HTML parses as XML. - -Then postprocess the XML differently based on the converter response. Which converters need XML postprocessing? GitLab appears to need a few fixes, suggesting a convention such as `xml`, e.g. `gitlabpostxml`. - -Finally, write the XML to file. Should self-closing tags such as `br` and `img` be converted back? - -### Turn GitLab output back to `$...$` - -GitLab parses math and returns it as a span, which is helpful. However, MathJax ultimately operates on `$...$` delimiters. Should the delimiters be restored? If so, should they replace the spans or be added alongside them? - -### Emojis as an extension - -cmark-gfm has an extension mechanism. Is emoji conversion implemented as an extension? If not, why not? - -Where are emojis converted, and where are they not converted? - -Can emoji conversion be turned on or off? Could that be an input to the MEX, and how should it be exposed to MATLAB? GitHub and GitLab always convert emojis, so perhaps it should be an MEX option that MATLAB always enables. - -### Math as an extension - -How is math rendering handled now? - -It appears to use regular expressions on the final HTML. Is that the best approach? Could math be handled at the XML level instead? Expressions may span multiple elements, and the converter introduces fake elements. - -Ultimately, math handling might be better implemented as a cmark extension. GitLab documents how it treats math in Markdown, which could provide a specification. Compare that with observed GitHub behavior. - -## DONE - -### Emoji shortcodes after fenced code blocks handle LF and CRLF - -With CRLF input, emoji shortcodes after a fenced code block were not expanded. The `src/emoji.c` preprocessor splits on LF but left the preceding CR in the line content. The closing-fence check rejected it and treated the rest of the document as code. - -The fix excludes a CR immediately before LF from structural parsing and preserves the original CRLF in the output. Regression tests cover shortcodes inside and after fenced code and in tables with both LF and CRLF. The MEX build and full MATLAB test suite passed (32 tests). - -Git's tracked files were mixed LF and CRLF; `core.autocrlf=true` caused CRLF worktree copies on Windows. MATLAB `fileread` preserves CRLF, while joining `readlines` with `newline()` produces LF. - -### JSON lookup fields use `from` - -The GitHub and GitLab configs now use `hostname.from` and `authentication.from` for the environment-variable or secrets-vault lookup identifiers. `apimd2html` reads the new fields; environment-first lookup, secrets fallback, and defaults are unchanged. The top-level `name` remains the service display label. Config tests verify the new identifiers and absence of the old nested fields. - -### Changelog documents public API host settings - -Replaced the Version 0.9.0 note about internal Markdown API helpers with separate GitHub and GitLab entries. They document `DOCMAKER_GITHUB_HOSTNAME` and `DOCMAKER_GITHUB_TOKEN` for the GitHub API, and `DOCMAKER_GITLAB_HOSTNAME` and `DOCMAKER_GITLAB_TOKEN` for the GitLab API, as environment variables or MATLAB Secrets. The conversion guide remains the detailed reference. diff --git a/src/cmark-gfm_mex.c b/src/cmark-gfm_mex.c index c022ae7..1808c02 100644 --- a/src/cmark-gfm_mex.c +++ b/src/cmark-gfm_mex.c @@ -16,8 +16,7 @@ #include "cmark-gfm.h" #include "cmark-gfm-core-extensions.h" -char *cmark_gfm_expand_emojis(const char *markdown, size_t length, - size_t *expanded_length); +void cmark_gfm_emoji_extension_ensure_registered(void); static int is_scalar_text(const mxArray *value) { @@ -50,16 +49,14 @@ static char *input_as_utf8(const mxArray *input, mxArray **temporary_char) void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { char *markdown; - char *expanded_markdown; char *html; mxArray *html_char; mxArray *temporary_char; - size_t expanded_length; cmark_parser *parser; cmark_node *document; cmark_syntax_extension *extension; static const char *extension_names[] = { - "table", "strikethrough", "autolink", "tasklist" + "table", "strikethrough", "autolink", "tasklist", "emoji" }; size_t i; int input_is_string; @@ -91,21 +88,15 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) "Unable to convert the Markdown input to UTF-8."); } - expanded_markdown = cmark_gfm_expand_emojis(markdown, strlen(markdown), - &expanded_length); - mxFree(markdown); if (temporary_char != NULL) { mxDestroyArray(temporary_char); } - if (expanded_markdown == NULL) { - mexErrMsgIdAndTxt("docmaker:OperationFailed", - "Unable to expand Markdown emoji shortcodes."); - } cmark_gfm_core_extensions_ensure_registered(); + cmark_gfm_emoji_extension_ensure_registered(); parser = cmark_parser_new(CMARK_OPT_DEFAULT); if (parser == NULL) { - free(expanded_markdown); + mxFree(markdown); mexErrMsgIdAndTxt("docmaker:OperationFailed", "Unable to allocate a cmark-gfm parser."); } @@ -114,15 +105,15 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) extension = cmark_find_syntax_extension(extension_names[i]); if (extension == NULL || !cmark_parser_attach_syntax_extension(parser, extension)) { cmark_parser_free(parser); - free(expanded_markdown); + mxFree(markdown); mexErrMsgIdAndTxt("docmaker:Unavailable", "Unable to enable the requested cmark-gfm extension."); } } - cmark_parser_feed(parser, expanded_markdown, expanded_length); + cmark_parser_feed(parser, markdown, strlen(markdown)); document = cmark_parser_finish(parser); - free(expanded_markdown); + mxFree(markdown); if (document == NULL) { cmark_parser_free(parser); diff --git a/src/emoji.c b/src/emoji.c index 0d40c91..73ad13e 100644 --- a/src/emoji.c +++ b/src/emoji.c @@ -1,17 +1,20 @@ /* - * GitHub emoji shortcode expansion for the cmark-gfm gateway. + * GitHub emoji shortcode extension for the cmark-gfm gateway. * * The shortcode table is generated by buildfile.m from - * ext/gemoji/db/emoji.json. - * The upstream cmark-gfm submodule is intentionally not modified. + * ext/gemoji/db/emoji.json. The upstream cmark-gfm submodule is intentionally + * not modified. * * Copyright 2026 The MathWorks, Inc. */ #include #include -#include -#include + +#include +#include +#include +#include #include "emoji_map.h" @@ -21,347 +24,110 @@ static int is_shortcode_char(unsigned char character) character == '+' || character == '-'; } -static size_t line_indent(const char *line, size_t length) +static cmark_node *match_emoji(cmark_syntax_extension *extension, + cmark_parser *parser, cmark_node *parent, unsigned char character, + cmark_inline_parser *inline_parser) { - size_t index = 0; - while (index < length && index < 3 && line[index] == ' ') { - ++index; + cmark_chunk *chunk; + const unsigned char *data; + const char *replacement; + cmark_node *node; + size_t offset; + size_t alias_end; + size_t consumed; + size_t i; + int start_column; + int start_line; + + (void)extension; + (void)parent; + + if (character != ':') { + return NULL; } - return index; -} - -static int is_fence_line(const char *line, size_t length, - char *fence_character, size_t *fence_length) -{ - size_t index = line_indent(line, length); - size_t run_start = index; - if (index >= length || (line[index] != '`' && line[index] != '~')) { - return 0; + chunk = cmark_inline_parser_get_chunk(inline_parser); + offset = (size_t)cmark_inline_parser_get_offset(inline_parser); + if (chunk == NULL || offset >= (size_t)chunk->len || + chunk->data[offset] != ':') { + return NULL; } - while (index < length && line[index] == line[run_start]) { - ++index; + + data = chunk->data + offset; + i = 1; + while (offset + i < (size_t)chunk->len && + is_shortcode_char(data[i])) { + ++i; } - if (index - run_start < 3) { - return 0; + if (i == 1 || offset + i >= (size_t)chunk->len || data[i] != ':') { + return NULL; } - *fence_character = line[run_start]; - *fence_length = index - run_start; - return 1; -} - -static int is_closing_fence_line(const char *line, size_t length, - char fence_character, size_t fence_length) -{ - char line_fence_character; - size_t line_fence_length; - size_t index; - if (!is_fence_line(line, length, &line_fence_character, - &line_fence_length) || - line_fence_character != fence_character || - line_fence_length < fence_length) { - return 0; + alias_end = i; + replacement = cmark_gfm_emoji_lookup((const char *)data + 1, + alias_end - 1); + if (replacement == NULL) { + return NULL; } - index = line_indent(line, length) + line_fence_length; - while (index < length && line[index] == ' ') { - ++index; + node = cmark_node_new_with_mem(CMARK_NODE_TEXT, parser->mem); + if (node == NULL) { + return NULL; + } + if (!cmark_node_set_literal(node, replacement)) { + cmark_node_free(node); + return NULL; } - return index == length; -} -static int append_bytes(char **output, size_t *length, size_t *capacity, - const char *bytes, size_t count) -{ - char *grown; - size_t required = *length + count + 1; + consumed = alias_end + 1; + start_line = cmark_inline_parser_get_line(inline_parser); + start_column = cmark_inline_parser_get_column(inline_parser); + node->start_line = node->end_line = start_line; + node->start_column = start_column; + node->end_column = start_column + (int)consumed - 1; + cmark_inline_parser_set_offset(inline_parser, (int)(offset + consumed)); - if (required > *capacity) { - while (*capacity < required) { - *capacity *= 2; - } - grown = (char *)realloc(*output, *capacity); - if (grown == NULL) { - return 0; - } - *output = grown; - } - memcpy(*output + *length, bytes, count); - *length += count; - (*output)[*length] = '\0'; - return 1; + return node; } -static int append_expanded_line(const char *line, size_t length, - char **output, size_t *output_length, size_t *output_capacity, - size_t *code_span_length) +static cmark_syntax_extension *create_emoji_extension(void) { - size_t index = 0; - - while (index < length) { - size_t start; - size_t alias_length; - size_t run_start; - int code_span_closed; - const char *replacement; - - if (*code_span_length != 0) { - start = index; - while (index < length) { - if (line[index] == '`') { - run_start = index++; - while (index < length && line[index] == '`') { - ++index; - } - if (index - run_start == *code_span_length) { - *code_span_length = 0; - break; - } - } else { - ++index; - } - } - if (!append_bytes(output, output_length, output_capacity, - line + start, index - start)) { - return 0; - } - if (*code_span_length != 0) { - return 1; - } - continue; - } - - if (line[index] == '`') { - start = index++; - while (index < length && line[index] == '`') { - ++index; - } - alias_length = index - start; - code_span_closed = 0; - while (index < length) { - if (line[index] == '`') { - run_start = index++; - while (index < length && line[index] == '`') { - ++index; - } - if (index - run_start == alias_length) { - code_span_closed = 1; - break; - } - } else { - ++index; - } - } - if (!code_span_closed) { - *code_span_length = alias_length; - } - if (!append_bytes(output, output_length, output_capacity, - line + start, index - start)) { - return 0; - } - if (!code_span_closed) { - return 1; - } - continue; - } - - if (line[index] == '<') { - start = index; - while (index < length && line[index] != '>') { - ++index; - } - if (index < length) { - ++index; - } - if (!append_bytes(output, output_length, output_capacity, - line + start, index - start)) { - return 0; - } - continue; - } + cmark_syntax_extension *extension; + cmark_llist *special_chars = NULL; + cmark_mem *mem = cmark_get_default_mem_allocator(); - if (line[index] == ']' && index + 1 < length && - line[index + 1] == '(') { - size_t parentheses = 1; - int angle_destination; - - start = index; - index += 2; - angle_destination = index < length && line[index] == '<'; - while (index < length && parentheses != 0) { - if (line[index] == '\\' && index + 1 < length) { - index += 2; - continue; - } - if (angle_destination) { - if (line[index] == '>') { - angle_destination = 0; - } - ++index; - continue; - } - if (parentheses == 1 && - (line[index] == ' ' || line[index] == '\t')) { - while (index < length && line[index] != ')') { - ++index; - } - if (index < length) { - ++index; - } - parentheses = 0; - } else if (line[index] == '(') { - ++parentheses; - ++index; - } else if (line[index] == ')') { - --parentheses; - ++index; - } else { - ++index; - } - } - if (!append_bytes(output, output_length, output_capacity, - line + start, index - start)) { - return 0; - } - continue; - } - - if (line[index] == ':' && - (index == 0 || line[index - 1] != '\\')) { - start = index + 1; - index = start; - while (index < length && is_shortcode_char((unsigned char)line[index])) { - ++index; - } - if (index > start && index < length && line[index] == ':') { - alias_length = index - start; - replacement = cmark_gfm_emoji_lookup(line + start, alias_length); - if (replacement != NULL) { - if (!append_bytes(output, output_length, output_capacity, - replacement, strlen(replacement))) { - return 0; - } - ++index; - continue; - } - } - index = start - 1; - } + extension = cmark_syntax_extension_new("emoji"); + if (extension == NULL) { + return NULL; + } - if (!append_bytes(output, output_length, output_capacity, - line + index, 1)) { - return 0; - } - ++index; + special_chars = cmark_llist_append(mem, special_chars, (void *)':'); + if (special_chars == NULL) { + cmark_syntax_extension_free(mem, extension); + return NULL; } - return 1; + + cmark_syntax_extension_set_match_inline_func(extension, match_emoji); + cmark_syntax_extension_set_special_inline_chars(extension, special_chars); + return extension; } -char *cmark_gfm_expand_emojis(const char *markdown, size_t length, - size_t *expanded_length) +static int register_emoji_extension(cmark_plugin *plugin) { - char *output; - size_t output_length = 0; - size_t output_capacity = length + 1; - size_t line_start = 0; - size_t code_span_length = 0; - char fence_character = '\0'; - size_t fence_length = 0; + cmark_syntax_extension *extension = create_emoji_extension(); - if (output_capacity < 64) { - output_capacity = 64; - } - output = (char *)malloc(output_capacity); - if (output == NULL) { - return NULL; - } - output[0] = '\0'; - if (length == 0) { - *expanded_length = 0; - return output; + if (extension == NULL) { + return 0; } + return cmark_plugin_register_syntax_extension(plugin, extension); +} - while (line_start < length || (length == 0 && line_start == 0)) { - size_t line_end = line_start; - size_t content_length; - int has_crlf; - int line_is_fence; - char line_fence_character = '\0'; - size_t line_fence_length = 0; - int fence_active = fence_length != 0; - - while (line_end < length && markdown[line_end] != '\n') { - ++line_end; - } - content_length = line_end - line_start; - has_crlf = line_end < length && content_length > 0 && - markdown[line_start + content_length - 1] == '\r'; - if (has_crlf) { - --content_length; - } - line_is_fence = is_fence_line(markdown + line_start, content_length, - &line_fence_character, &line_fence_length); - - if (fence_active) { - if (is_closing_fence_line(markdown + line_start, content_length, - fence_character, fence_length)) { - if (!append_bytes(&output, &output_length, &output_capacity, - markdown + line_start, content_length)) { - free(output); - return NULL; - } - fence_length = 0; - } else if (!append_bytes(&output, &output_length, &output_capacity, - markdown + line_start, content_length)) { - free(output); - return NULL; - } - } else if (line_is_fence) { - code_span_length = 0; - fence_character = line_fence_character; - fence_length = line_fence_length; - if (!append_bytes(&output, &output_length, &output_capacity, - markdown + line_start, content_length)) { - free(output); - return NULL; - } - } else if (content_length == 0 || markdown[line_start] == '\t' || - (content_length >= 4 && - markdown[line_start] == ' ' && markdown[line_start + 1] == ' ' && - markdown[line_start + 2] == ' ' && markdown[line_start + 3] == ' ')) { - code_span_length = 0; - if (!append_bytes(&output, &output_length, &output_capacity, - markdown + line_start, content_length)) { - free(output); - return NULL; - } - } else { - if (!append_expanded_line(markdown + line_start, content_length, - &output, &output_length, &output_capacity, - &code_span_length)) { - free(output); - return NULL; - } - } +void cmark_gfm_emoji_extension_ensure_registered(void) +{ + static int registered = 0; - if (line_end < length) { - if (has_crlf && !append_bytes(&output, &output_length, - &output_capacity, "\r", 1)) { - free(output); - return NULL; - } - if (!append_bytes(&output, &output_length, &output_capacity, - "\n", 1)) { - free(output); - return NULL; - } - line_start = line_end + 1; - } else { - line_start = length; - } + if (!registered) { + cmark_register_plugin(register_emoji_extension); + registered = 1; } - - *expanded_length = output_length; - return output; } diff --git a/tests/tMarkdownConversion.m b/tests/tMarkdownConversion.m index 7b006bf..617c5d8 100644 --- a/tests/tMarkdownConversion.m +++ b/tests/tMarkdownConversion.m @@ -95,15 +95,104 @@ function tCmarkMexPreservesEmojiInsideMultilineCodeSpan( testCase ) end % tCmarkMexPreservesEmojiInsideMultilineCodeSpan + function tCmarkMexPreservesEmojiInsideIndentedCode( testCase ) + + html = string( docmaker.cmarkmd2html( " :smile:" ) ); + + testCase.verifyTrue( contains( html, ":smile:" ) ) + testCase.verifyFalse( contains( html, "😄" ) ) + + end % tCmarkMexPreservesEmojiInsideIndentedCode + + function tCmarkMexPreservesEmojiInTildeFenceInBlockquote( testCase ) + + markdown = strjoin( [ ... + "> ~~~matlab"; ... + "> :smile:"; ... + "> ~~~" ], newline() ); + html = string( docmaker.cmarkmd2html( markdown ) ); + + testCase.verifyTrue( contains( html, ":smile:" ) ) + testCase.verifyFalse( contains( html, "😄" ) ) + + end % tCmarkMexPreservesEmojiInTildeFenceInBlockquote + + function tCmarkMexPreservesEmojiInTildeFenceInList( testCase ) + + markdown = strjoin( [ ... + "- ~~~matlab"; ... + " :smile:"; ... + " ~~~" ], newline() ); + html = string( docmaker.cmarkmd2html( markdown ) ); + + testCase.verifyTrue( contains( html, ":smile:" ) ) + testCase.verifyFalse( contains( html, "😄" ) ) + + end % tCmarkMexPreservesEmojiInTildeFenceInList + + function tCmarkMexPreservesEscapedEmojiShortcode( testCase ) + + markdown = string( char( 92 ) ) + ":smile:"; + html = string( docmaker.cmarkmd2html( markdown ) ); + + testCase.verifyTrue( contains( html, ":smile:" ) ) + testCase.verifyFalse( contains( html, "😄" ) ) + + end % tCmarkMexPreservesEscapedEmojiShortcode + + function tCmarkMexPreservesUnknownEmojiShortcode( testCase ) + + html = string( docmaker.cmarkmd2html( ":not_a_real_emoji:" ) ); + + testCase.verifyTrue( contains( html, ":not_a_real_emoji:" ) ) + + end % tCmarkMexPreservesUnknownEmojiShortcode + + function tCmarkMexExpandsEmojiInsideHtmlText( testCase ) + + markdown = ":smile:"; + html = string( docmaker.cmarkmd2html( markdown ) ); + + testCase.verifyTrue( contains( html, "😄" ) ) + + end % tCmarkMexExpandsEmojiInsideHtmlText + function tCmarkMexPreservesEmojiInsideLinkDestination( testCase ) html = string( docmaker.cmarkmd2html( ... - "[link](path_(part):smile:)" ) ); + "[link :smile:](path_(part):smile:)" ) ); testCase.verifyTrue( contains( html, ":smile:" ) ) + testCase.verifyTrue( contains( html, "😄" ) ) end % tCmarkMexPreservesEmojiInsideLinkDestination + function tCmarkMexPreservesEmojiInReferenceDestination( testCase ) + + markdown = strjoin( [ ... + "[link :smile:][id]"; ... + ""; ... + "[id]: https://example.com/:smile:" ], newline() ); + html = string( docmaker.cmarkmd2html( markdown ) ); + + testCase.verifyTrue( contains( ... + html, 'href="https://example.com/:smile:"' ) ) + testCase.verifyTrue( contains( html, "😄" ) ) + + end % tCmarkMexPreservesEmojiInReferenceDestination + + function tCmarkMexPreservesEmojiInBareUrl( testCase ) + + html = string( docmaker.cmarkmd2html( ... + "https://example.com/:smile:" ) ); + + testCase.verifyTrue( contains( ... + html, 'href="https://example.com/:smile"' ) ) + testCase.verifyTrue( contains( html, ":smile" ) ) + testCase.verifyFalse( contains( html, "😄" ) ) + + end % tCmarkMexPreservesEmojiInBareUrl + function tHostnameAcceptsDnsNames( testCase ) hosts = [ ...