Skip to content

Commit

Permalink
feat: add underline support
Browse files Browse the repository at this point in the history
  • Loading branch information
Heinrich-XIAO committed Feb 4, 2025
1 parent e2663ed commit 6f70933
Showing 1 changed file with 55 additions and 8 deletions.
63 changes: 55 additions & 8 deletions core/Util/Util.vala
Original file line number Diff line number Diff line change
Expand Up @@ -1098,13 +1098,18 @@ We hope you’ll enjoy using Planify!""");
var text = escape_text (_text);

try {
Regex mailto_regex = /(?P<mailto>[a-zA-Z0-9\._\%\+\-]+@[a-zA-Z0-9\-\.]+\.[a-zA-Z]+(\S*))/; // vala-lint=space-before-paren
Regex url_regex = /(?P<url>(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]+(\/\S*))/; // vala-lint=space-before-paren
Regex mailto_regex = /(?P<mailto>[a-zA-Z0-9\._\%\+\-]+@[a-zA-Z0-9\-\.]+\.[a-zA-Z]+(\S*))/;
Regex url_regex = /(?P<url>(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]+(\/\S*))/;
Regex url_markdown = new Regex ("\\[([^\\]]+)\\]\\(([^\\)]+)\\)");

Regex italic_bold_regex = /\*\*\*(.*?)\*\*\*/; // vala-lint=space-before-paren
Regex bold_regex = /\*\*(.*?)\*\*/; // vala-lint=space-before-paren
Regex italic_regex = /\*(.*?)\*/; // vala-lint=space-before-paren

Regex italic_bold_regex = /\*\*\*(.*?)\*\*\*/;
Regex bold_regex = /\*\*(.*?)\*\*/;
Regex italic_regex = /\*(.*?)\*/;
Regex underline_regex = /_(.*?)_/;

Regex italic_bold_underline_regex = /\*\*\*_([^*]+)_\*\*\*/;
Regex bold_underline_regex = /\*\*_([^*]+)_\*\*/;
Regex italic_underline_regex = /\*_(.*?)_\*/;

MatchInfo info;

Expand All @@ -1127,7 +1132,6 @@ We hope you’ll enjoy using Planify!""");
if (url_regex.match (text, 0, out info)) {
do {
var url = info.fetch_named ("url");

if (!url_exists (url, markdown_urls)) {
urls.append (url);
}
Expand Down Expand Up @@ -1155,12 +1159,39 @@ We hope you’ll enjoy using Planify!""");
} while (info.next ());
}

Gee.ArrayList<RegexMarkdown> italic_bold_underline = new Gee.ArrayList<RegexMarkdown> ();
if (italic_bold_underline_regex.match (text, 0, out info)) {
do {
italic_bold_underline.add (new RegexMarkdown (info.fetch (0), info.fetch (1)));
} while (info.next ());
}

Gee.ArrayList<RegexMarkdown> bold_underline = new Gee.ArrayList<RegexMarkdown> ();
if (bold_underline_regex.match (text, 0, out info)) {
do {
bold_underline.add (new RegexMarkdown (info.fetch (0), info.fetch (1)));
} while (info.next ());
}

Gee.ArrayList<RegexMarkdown> italic_underline = new Gee.ArrayList<RegexMarkdown> ();
if (italic_underline_regex.match (text, 0, out info)) {
do {
italic_underline.add (new RegexMarkdown (info.fetch (0), info.fetch (1)));
} while (info.next ());
}

Gee.ArrayList<RegexMarkdown> underlines = new Gee.ArrayList<RegexMarkdown> ();
if (underline_regex.match (text, 0, out info)) {
do {
underlines.add (new RegexMarkdown (info.fetch (0), info.fetch (1)));
} while (info.next ());
}

string converted = text;

foreach (RegexMarkdown m in markdown_urls) {
string markdown_text = m.text;
string markdown_link = m.extra;

string urlAsLink = @"<a href=\"$markdown_link\">$markdown_text</a>";
converted = converted.replace (m.match, urlAsLink);
}
Expand All @@ -1173,6 +1204,22 @@ We hope you’ll enjoy using Planify!""");
converted = converted.replace (email, @"<a href=\"mailto:$email\">$email</a>");
});

foreach (RegexMarkdown m in italic_bold_underline) {
converted = converted.replace (m.match, "<i><b><u>" + m.text + "</u></b></i>");
}

foreach (RegexMarkdown m in bold_underline) {
converted = converted.replace (m.match, "<b><u>" + m.text + "</u></b>");
}

foreach (RegexMarkdown m in italic_underline) {
converted = converted.replace (m.match, "<i><u>" + m.text + "</u></i>");
}

foreach (RegexMarkdown m in underlines) {
converted = converted.replace (m.match, "<u>" + m.text + "</u>");
}

foreach (RegexMarkdown m in italic_bold) {
converted = converted.replace (m.match, "<i><b>" + m.text + "</b></i>");
}
Expand Down

0 comments on commit 6f70933

Please sign in to comment.