-
Notifications
You must be signed in to change notification settings - Fork 8
/
pkgdown-tweaks.R
45 lines (36 loc) · 1.17 KB
/
pkgdown-tweaks.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# inserts plausible.io trackers into pkgdown site pages
site <- "jaysire.djnavarro.net"
# lines to be inserted into header
new_lines <- c(
' ',
'<!-- adds plausible.io -->',
paste0('<script async defer data-domain="', site, '" src="https://plausible.io/js/plausible.js"></script>'),
' '
)
# files into which lines need insertion
html_files <- list.files(
path = here::here("docs"),
pattern = "html$",
recursive = TRUE,
full.names = TRUE
)
# function to insert lines
insert_lines <- function(file, new_lines) {
lines <- brio::read_lines(file)
ind <- stringr::str_which(lines, "</head>")
if(is.null(ind)) rlang::warn(paste0("no </head> line found in: ", file))
if(length(ind) > 1) rlang::warn(paste0("multiple </head> lines found in: ", file))
# don't add lines if the tracker is already there
tracker_ind <- stringr::str_which(lines, stringr::fixed(site))
if(length(tracker_ind) == 0) {
# assume file doesn't begin or end with </head>
lines <- c(
lines[1:(ind[1]-1)],
new_lines,
lines[ind[1]:length(lines)]
)
brio::write_lines(lines, file)
}
}
# insert for all files
purrr::walk(html_files, insert_lines, new_lines = new_lines)