1- # ' fixes link-check problem
2- # ' Finds whether any files use the string: "(.mailto:" for linking email adresses
3- # ' then just replaces with: "(mailto:" as is markdown standard
1+ # ' Fixes link-check problem
2+ # ' - Finds files that use the string "(.mailto:" and corrects to "(mailto:"
3+ # ' - Updates links to `.qmd` files to point to `.md` files.
44# '
55# ' @param file_list vector of filenames
66# '
7- # ' @return messages only. side effect is: changes files.
7+ # ' @return messages. Side effect: modifies files.
88modify_files <- function (file_list ) {
9- # Create an empty vector to store the file names that contain the string
9+ # Create an empty vector to store file names that need modifications
1010 matching_files <- c()
1111
12- # Iterate over each file in the directory
12+ # Iterate over each file to check for issues
1313 for (file in file_list ) {
1414 # Read the contents of the file
1515 file_contents <- readLines(file )
1616
17- # Check if the file contains the string "(.mailto:"
18- if (any(grepl(" \\ (\\ .mailto:" , file_contents ))) {
17+ # Check if the file contains the string "(.mailto:" OR references to `.qmd` files
18+ if (any(grepl(" \\ (\\ .mailto:" , file_contents )) || any(grepl( " \\ .qmd \\ ) " , file_contents )) ) {
1919 # Add the file name to the vector
2020 matching_files <- c(matching_files , file )
2121 }
2222 }
2323
24- # Iterate over the matching files
24+ # Iterate over the matching files to apply fixes
2525 for (file in matching_files ) {
2626 # Read the contents of the file
2727 file_contents <- readLines(file )
2828
29- # Remove the "." from each line that contains the string
29+ # Fix email links
3030 modified_contents <- gsub(" \\ (\\ .mailto:" , " (mailto:" , file_contents )
3131
32+ # Update links to `.qmd` files to point to `.md` files
33+ # Matches patterns like `[text](filename.qmd)` and replaces `.qmd` with `.md`
34+ modified_contents <- gsub(" \\ .qmd\\ )" , " .md)" , modified_contents )
35+
3236 # Write the modified contents back to the file
3337 writeLines(modified_contents , file )
3438
35- # Print a message indicating the modification has been made
39+ # Print a message indicating what modifications have been made
3640 message(" Modified file:" , file , " \n " )
3741 }
3842
39- # Print the list of matching files
43+ # Print a list of matching files that were modified
4044 message(" Matching files:" , matching_files , " \n " )
4145}
4246
43- # get all qmd files
47+ # Get all `. qmd` files
4448all_qmd <- list.files(full.names = FALSE , all.files = FALSE , pattern = " .qmd$" , recursive = TRUE )
4549
46- # modify if needed the link to email problem for link checker
50+ # Modify files to fix email links and update `.qmd` references
4751modify_files(all_qmd )
48- # get filenames ending with .md
52+
53+ # Generate a list of `.md` filenames that will replace `.qmd` files
4954all_md <- gsub(" .qmd$" , " .md" , all_qmd )
50- # rename all files
51- file.rename(all_qmd , all_md )
55+
56+ # Rename all `.qmd` files to `.md`
57+ file.rename(all_qmd , all_md )
0 commit comments