-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.qmd
69 lines (60 loc) · 1.96 KB
/
index.qmd
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
---
title: Welcome to the pharmaverse blog!
subtitle: Short, fun and user-driven content around the pharmaverse.
listing:
feed: true
contents: posts
sort: "date desc"
type: default
image-height: 220px
page-size: 100
categories: true
sort-ui: true
filter-ui: true
page-layout: full
margin-header: Get notified of new posts by signing up to our [periodic newsletter](https://forms.office.com/e/AJu4xWEGJx) or by joining [pharmaverse/slack](https://pharmaverse.slack.com). Be sure to check out [https://www.r-bloggers.com]( https://www.r-bloggers.com) as well.
title-block-banner: true
css: styles.css
---
```{r write-redirects}
#| echo: false
# list names of post folders
posts <- list.dirs(
path = here::here("posts"),
full.names = FALSE,
recursive = FALSE
)
# extract the slugs
slugs <- sub(".*?_", "", posts)
# lines to insert to a netlify _redirect file
post_redirects <- paste0("/", slugs, " ", "/posts/", posts)
# function to extract post categories from index.qmd files
get_post_categories <- function(post) {
file <- here::here("posts", post) |> list.files(pattern = ".qmd", full.names = TRUE, recursive = TRUE)
if (is.null(file) || is.na(fs::file_exists(file)) || !fs::file_exists(file)) {
return(character(0))
}
file |>
readLines() |>
stringr::str_subset("^categories:") |>
stringr::str_extract("\\[.*\\]") |>
stringr::str_remove_all("[\\[\\]]") |>
stringr::str_split(", ") |>
unlist()
}
# extract the categories
categories <- purrr::map(posts, get_post_categories) |>
unlist() |>
unique()
# extract categories in nice form
keys <- categories |>
stringr::str_to_lower() |>
stringr::str_replace_all(" ", "-")
# extract categories in ugly form
values <- categories |>
stringr::str_replace_all(" ", "%20")
# category redirects
category_redirects <- paste0("/category/", keys, " ", "/#category=", values)
# write the _redirect file
writeLines(c(post_redirects, category_redirects), here::here("_site", "_redirects"))
```