Skip to content

Commit b356dcb

Browse files
committed
better link capturing of internal links
1 parent edab371 commit b356dcb

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

tools/od_ref_bot/src/main.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ fn format_embed(page: &str, data: &Data) -> Option<serenity::CreateEmbed> {
167167
.title(&title)
168168
.url(get_url(page, parsed))
169169
.color(Colour::from_rgb(246, 114, 128))
170-
.description(format_body(body));
170+
.description(format_body(body, data));
171171

172172
let extra = parsed.extra.as_ref().unwrap();
173173

@@ -254,7 +254,7 @@ fn format_embed(page: &str, data: &Data) -> Option<serenity::CreateEmbed> {
254254
}
255255

256256
/// Converts the Tera-formatted markdown into more Discord compatible markdown.
257-
fn format_body(body: &str) -> String {
257+
fn format_body(body: &str, data: &Data) -> String {
258258
let mut replaced_body = body.to_string();
259259

260260
let link_finder_regex =
@@ -286,12 +286,24 @@ fn format_body(body: &str) -> String {
286286
)
287287
}
288288

289+
let mut new_body = replaced_body.clone();
290+
291+
let internal_link_regex = Regex::new(r"\[(.*?)]\(@/(.*?)\)").unwrap();
292+
for capture in internal_link_regex.captures_iter(&replaced_body) {
293+
let original = capture.get(0).unwrap().as_str();
294+
295+
let display = capture.get(1).unwrap().as_str();
296+
let path = capture.get(2).unwrap().as_str();
297+
298+
let url = get_url(path, data.path_to_parsed.get(path).unwrap());
299+
300+
new_body = new_body.replace(original, format!("[{}]({})", display, url).as_str());
301+
}
302+
289303
let tag_cleaner_regex = Regex::new(r"\{\{.*?}}|\{%.*?%}").unwrap();
290304

291-
replaced_body = tag_cleaner_regex
292-
.replace_all(&replaced_body, "")
293-
.to_string();
294-
replaced_body.replace("```dm", "```js")
305+
new_body = tag_cleaner_regex.replace_all(&new_body, "").to_string();
306+
new_body.replace("```dm", "```js")
295307
}
296308

297309
/// Converts the internal Zola page structure into something we can link to.

0 commit comments

Comments
 (0)