fix: assign unique IDs to gist permanent links

This commit is contained in:
Aravinth Manivannan 2024-04-13 06:16:32 +05:30
parent defe361077
commit dcdc2051cc
Signed by untrusted user: realaravinth
GPG key ID: F8F50389936984FF
4 changed files with 30 additions and 5 deletions

20
Cargo.lock generated
View file

@ -924,6 +924,12 @@ version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7"
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]] [[package]]
name = "home" name = "home"
version = "0.5.5" version = "0.5.5"
@ -1170,6 +1176,7 @@ dependencies = [
"sailfish", "sailfish",
"serde", "serde",
"serde_json", "serde_json",
"sha256",
"sled", "sled",
"syntect", "syntect",
"url", "url",
@ -1963,6 +1970,19 @@ dependencies = [
"digest", "digest",
] ]
[[package]]
name = "sha256"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18278f6a914fa3070aa316493f7d2ddfb9ac86ebc06fa3b83bffda487e9065b0"
dependencies = [
"async-trait",
"bytes",
"hex",
"sha2",
"tokio",
]
[[package]] [[package]]
name = "signal-hook-registry" name = "signal-hook-registry"
version = "1.4.1" version = "1.4.1"

View file

@ -30,6 +30,7 @@ sled = "0.34.7"
syntect = "5.0.0" syntect = "5.0.0"
url = "2.2" url = "2.2"
actix-web-codegen-const-routes = "0.2.0" actix-web-codegen-const-routes = "0.2.0"
sha256 = "1.5.0"
[dependencies.graphql_client] [dependencies.graphql_client]
features = ["reqwest"] features = ["reqwest"]

View file

@ -21,6 +21,7 @@ use graphql_client::{reqwest::post_graphql, GraphQLQuery};
use reqwest::header::USER_AGENT; use reqwest::header::USER_AGENT;
use reqwest::Client; use reqwest::Client;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sha256::digest;
use sled::{Db, Tree}; use sled::{Db, Tree};
use crate::proxy::StringUtils; use crate::proxy::StringUtils;
@ -287,19 +288,21 @@ impl Data {
filepath: &file.file_name, filepath: &file.file_name,
code: &file.content, code: &file.content,
}; };
file.content = highlight.syntax_highlight(); file.content = highlight.syntax_highlight(&digest(&file.raw_url));
files.push(file); files.push(file);
GistContent { GistContent {
files, files,
html_url: gist_url, html_url: gist_url,
} }
} else { } else {
let mut index = 1;
gist.files.iter_mut().for_each(|f| { gist.files.iter_mut().for_each(|f| {
let highlight = render_html::SourcegraphQuery { let highlight = render_html::SourcegraphQuery {
filepath: &f.file_name, filepath: &f.file_name,
code: &f.content, code: &f.content,
}; };
f.content = highlight.syntax_highlight(); f.content = highlight.syntax_highlight(&digest(&f.raw_url));
index += 1;
}); });
gist gist
}; };

View file

@ -38,7 +38,7 @@ pub struct SourcegraphQuery<'a> {
} }
impl<'a> SourcegraphQuery<'a> { impl<'a> SourcegraphQuery<'a> {
pub fn syntax_highlight(&self) -> String { pub fn syntax_highlight(&self, gist_name: &str) -> String {
// let ss = SYNTAX_SET; // let ss = SYNTAX_SET;
let ts = ThemeSet::load_defaults(); let ts = ThemeSet::load_defaults();
@ -66,7 +66,8 @@ impl<'a> SourcegraphQuery<'a> {
if line_num == 0 || line_num == total_lines - 1 { if line_num == 0 || line_num == total_lines - 1 {
output.push_str(line); output.push_str(line);
} else { } else {
output.push_str(&format!("<div title='click for more options' id=\"line-{num}\"class=\"line\"><details class='line_links'><summary class='line_top-link'><a href=\"#line-{num}\"<span class=\"line-number\">{num}</span></a>{line}</summary><a href=\"#line-{num}\"<span class=\"line-link\">Permanant link</span></a><a href=\"#line-{num}\"<span class=\"line-link\">Highlight</span></a></details></div>" let line_id = format!("{gist_name}-{num}");
output.push_str(&format!("<div title='click for more options' id=\"line-{line_id}\"class=\"line\"><details class='line_links'><summary class='line_top-link'><a href=\"#line-{line_id}\"<span class=\"line-number\">{num}</span></a>{line}</summary><a href=\"#line-{line_id}\"<span class=\"line-link\">Permanant link</span></a><a href=\"#line-{line_id}\"<span class=\"line-link\">Highlight</span></a></details></div>"
)); ));
num += 1; num += 1;
} }
@ -153,7 +154,7 @@ mod tests {
}; };
let result = query.determine_language(&syntax_set); let result = query.determine_language(&syntax_set);
assert_eq!(result.name, "TeX"); assert_eq!(result.name, "TeX");
let _result = query.syntax_highlight(); let _result = query.syntax_highlight("foo");
} }
//#[test] //#[test]