Skip to content

Commit

Permalink
fix(graindoc): Escape * chars that would close markdown bold incorrec…
Browse files Browse the repository at this point in the history
…tly (grain-lang#1685)

fix(graindoc): Escape * characters that would close markdown bold incorrectly
  • Loading branch information
phated authored and av8ta committed Apr 11, 2023
1 parent bf78a6f commit 88ff31c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
11 changes: 10 additions & 1 deletion compiler/src/utils/markdown.re
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ let frontmatter = rows => {
};

let bold = str => {
Format.sprintf("**%s**", str);
let escaped_str =
Str.global_substitute(
Str.regexp({|\(^\*+\)\|\(\*\*+\)\|\(\*+$\)|}),
str => {
let matched = Str.matched_string(str);
Str.global_replace(Str.regexp({|\*|}), {|\*|}, matched);
},
str,
);
Format.sprintf("**%s**", escaped_str);
};

let blockquote = str => {
Expand Down
42 changes: 42 additions & 0 deletions compiler/test/utils/markdown.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
open Grain_utils;

open Grain_tests.TestFramework;
open Grain_tests.Runner;

describe("utils/markdown", ({test}) => {
test("bold_escape_leading_star", ({expect}) => {
expect.string(Markdown.bold("*")).toEqual({|**\***|})
});

test("bold_escape_many_leading_stars", ({expect}) => {
expect.string(Markdown.bold("*****")).toEqual({|**\*\*\*\*\***|})
});

// This is how an operator is passed
test("bold_no_escape_one_nonleading_star", ({expect}) => {
expect.string(Markdown.bold("(*)")).toEqual({|**(*)**|})
});

// This is the pow operator
test("bold_escape_two_nonleading_star", ({expect}) => {
expect.string(Markdown.bold("(**)")).toEqual({|**(\*\*)**|})
});

test("bold_escape_many_nonleading_star", ({expect}) => {
expect.string(Markdown.bold("(******)")).toEqual({|**(\*\*\*\*\*\*)**|})
});

test("bold_escape_trailing_star", ({expect}) => {
expect.string(Markdown.bold("foo*")).toEqual({|**foo\***|})
});

test("bold_escape_many_trailing_star", ({expect}) => {
expect.string(Markdown.bold("foo*****")).toEqual({|**foo\*\*\*\*\***|})
});

test("bold_escape_many_leading_and_trailing_star", ({expect}) => {
expect.string(Markdown.bold("*****foo*****")).toEqual(
{|**\*\*\*\*\*foo\*\*\*\*\***|},
)
});
});

0 comments on commit 88ff31c

Please sign in to comment.