Skip to content

Commit dc407d1

Browse files
saeckielegaanz
authored andcommitted
Address review comments
1 parent 2c35216 commit dc407d1

1 file changed

Lines changed: 36 additions & 40 deletions

File tree

src/check/readme.rs

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::Path;
33
use std::sync::LazyLock;
44
use std::{collections::HashSet, ops::Range};
55

6-
use codespan_reporting::diagnostic::{Diagnostic, Label, Severity};
6+
use codespan_reporting::diagnostic::{Diagnostic, Label};
77
use comrak::nodes::{LineColumn, NodeList, NodeValue as MdNode, Sourcepos};
88
use html5ever::tendril::TendrilSink;
99
use regex::Regex;
@@ -62,7 +62,7 @@ pub async fn check_readme(
6262
.with_code("readme/unsupported-extension/alert")
6363
.with_message("GFM alert boxes are not supported on Typst Universe.")
6464
.with_labels(vec![Label::primary(
65-
readme_fake_file_id(),
65+
readme_file_id(),
6666
sourcepos_to_range(&readme, md_node.sourcepos),
6767
)]),
6868
);
@@ -76,7 +76,7 @@ pub async fn check_readme(
7676
.with_code("readme/unsupported-extension/tasklist")
7777
.with_message("GFM task lists are not supported on Typst Universe.")
7878
.with_label(Label::primary(
79-
readme_fake_file_id(),
79+
readme_file_id(),
8080
sourcepos_to_range(&readme, md_node.sourcepos),
8181
)),
8282
);
@@ -126,9 +126,13 @@ fn check_readme_code_block(
126126
return;
127127
}
128128

129+
// It is important to create a fake ID for each markdown code block. This
130+
// allows having multiple sources with the same file path, since the README
131+
// can contain multiple Typst example blocks.
132+
let readme_code_block_id = FileId::new_fake(VirtualPath::new("README.md"));
129133
let source = world
130134
.virtual_source(
131-
readme_fake_file_id(),
135+
readme_code_block_id,
132136
Bytes::from_string(code_block.literal.clone()),
133137
sourcepos.start.line,
134138
)
@@ -264,7 +268,7 @@ fn check_readme_link_url(
264268
More details: https://github.com/typst/packages/blob/main/docs/tips.md#what-to-commit-what-to-exclude",
265269
))
266270
.with_labels(vec![Label::primary(
267-
readme_fake_file_id(),
271+
readme_file_id(),
268272
sourcepos_to_range(readme, sourcepos),
269273
)]),
270274
);
@@ -274,12 +278,7 @@ fn check_readme_link_url(
274278

275279
const DEFAULT_BRANCHES: [&str; 2] = ["main", "master"];
276280

277-
fn check_repo_file_url(
278-
diags: &mut Diagnostics,
279-
readme: &str,
280-
sourcepos: Sourcepos,
281-
url: &str,
282-
) -> Option<()> {
281+
fn check_repo_file_url(diags: &mut Diagnostics, readme: &str, sourcepos: Sourcepos, url: &str) {
283282
static GITHUB_URL: LazyLock<Regex> = LazyLock::new(|| {
284283
Regex::new(r"https://github.com/([^/]+)/([^/]+)/(?:blob|tree)/([^/]+)/(.+)").unwrap()
285284
});
@@ -311,7 +310,7 @@ fn check_repo_file_url(
311310
} else if let Some(captures) = CODEBERG_URL.captures(url) {
312311
(Host::Codeberg, captures)
313312
} else {
314-
return None;
313+
return;
315314
};
316315

317316
let user = captures.get(1).unwrap().as_str();
@@ -320,7 +319,7 @@ fn check_repo_file_url(
320319
let path = captures.get(4).unwrap().as_str();
321320

322321
if !DEFAULT_BRANCHES.contains(&branch) {
323-
return None;
322+
return;
324323
}
325324

326325
let name = match host {
@@ -337,7 +336,7 @@ fn check_repo_file_url(
337336

338337
diags.emit(
339338
Diagnostic::warning()
340-
.with_code("readme/link/github-url-permalink")
339+
.with_code("readme/link/repository-url-permalink")
341340
.with_message(format_args!(
342341
"{name} URL links to default branch: `{url}`.\n\n\
343342
Consider using a link to a specific tag/release or a permalink to a commit instead. \
@@ -347,12 +346,10 @@ fn check_repo_file_url(
347346
is already present in the submitted package."
348347
))
349348
.with_label(Label::primary(
350-
readme_fake_file_id(),
349+
readme_file_id(),
351350
sourcepos_to_range(readme, sourcepos),
352351
)),
353352
);
354-
355-
Some(())
356353
}
357354

358355
fn check_image_alternative_description(
@@ -368,27 +365,29 @@ fn check_image_alternative_description(
368365
static SINGLE_LETTER: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^\w$").unwrap());
369366

370367
let alt = alt.trim();
371-
372-
if alt.is_empty() || SINGLE_WORD.is_match(alt) || SINGLE_LETTER.is_match(alt) {
373-
let (severity, message) = if alt.is_empty() {
374-
let message = String::from(
375-
"Missing alternative description for image. \
376-
Please add a short description to make this image more accessible.",
377-
);
378-
(Severity::Error, message)
379-
} else {
380-
let message = format!(
381-
"Possibly inadequate alternative description for image: `{alt}`. \
382-
Please add a short description to make this image more accessible.",
383-
);
384-
(Severity::Warning, message)
385-
};
368+
if alt.is_empty() {
386369
diags.emit(
387-
Diagnostic::new(severity)
370+
Diagnostic::error()
388371
.with_code("readme/image/missing-alt")
389-
.with_message(message)
372+
.with_message(
373+
"Missing alternative description for image. \
374+
Please add a short description to make this image more accessible.",
375+
)
376+
.with_label(Label::primary(
377+
readme_file_id(),
378+
sourcepos_to_range(readme, sourcepos),
379+
)),
380+
);
381+
} else if SINGLE_WORD.is_match(alt) || SINGLE_LETTER.is_match(alt) {
382+
diags.emit(
383+
Diagnostic::warning()
384+
.with_code("readme/image/inadequate-alt")
385+
.with_message(format_args!(
386+
"Possibly inadequate alternative description for image: `{alt}`. \
387+
Please add a short description to make this image more accessible."
388+
))
390389
.with_label(Label::primary(
391-
readme_fake_file_id(),
390+
readme_file_id(),
392391
sourcepos_to_range(readme, sourcepos),
393392
)),
394393
);
@@ -415,9 +414,6 @@ fn sourcepos_to_range(s: &str, pos: Sourcepos) -> Range<usize> {
415414
start..end
416415
}
417416

418-
/// It is important to create a fake ID for each node, this allows to
419-
/// have multiple sources with the same file path, which is useful here
420-
/// since the README can contain multiple Typst example blocks.
421-
fn readme_fake_file_id() -> FileId {
422-
FileId::new_fake(VirtualPath::new("README.md"))
417+
fn readme_file_id() -> FileId {
418+
FileId::new(None, VirtualPath::new("README.md"))
423419
}

0 commit comments

Comments
 (0)