Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change permalink for English posts #37

Merged
merged 9 commits into from
Jul 11, 2021
Merged

Change permalink for English posts #37

merged 9 commits into from
Jul 11, 2021

Conversation

mincong-h
Copy link
Owner

@mincong-h mincong-h commented Jul 11, 2021

Overview

Change permalink for English posts so that English posts and Chinese posts are aligned.

- permalink   : /:year/:month/:day/:title/
+ permalink   : /en/:title/

New URL pattern for English posts (collection posts) and Chinese posts (collection cn):

en: /en/:title/
cn: /cn/:title/

Existing posts are kept for backward compatibility. It only applies to new posts that will be created after this PR. Why? Mainly because I don't want to break the comment system (Disqus) and Google Analytics. And I don't want to spend too much time on the migration since it's not the top priority.

Migration

Script used for hard-coding the URL for existing posts:

#!/bin/bash
paths=($(find "${HOME}/github/mincong-h.github.io/_posts" -type f -name "*.md" | tr '\n' ' '))
i=0
for path in "${paths[@]}"
do
    filename="${path##*/}"
    year=$(echo $filename  | sed -E 's/^([[:digit:]]+)-([[:digit:]]+)-([[:digit:]]+)-(.*)\.md/\1/')
    month=$(echo $filename | sed -E 's/^([[:digit:]]+)-([[:digit:]]+)-([[:digit:]]+)-(.*)\.md/\2/')
    day=$(echo $filename   | sed -E 's/^([[:digit:]]+)-([[:digit:]]+)-([[:digit:]]+)-(.*)\.md/\3/')
    name=$(echo $filename  | sed -E 's/^([[:digit:]]+)-([[:digit:]]+)-([[:digit:]]+)-(.*)\.md/\4/')
    permalink="/${year}/${month}/${day}/${name}/"
    echo "${i}: year=${year}, month=${month}, day=${day}, name=${name}, permalink=${permalink}"
    sed -i '' -E '/comments:/i\
permalink:         PERMALINK
' "$path"
    sed -i '' "s|PERMALINK|${permalink}|" "$path"
    i=$((i + 1))
done

Testing

New post uses new permalink:

➜  mincong-h.github.io git:(permalink u=) ./newpost.sh Test Me
Blog created: /Users/minconghuang/github/mincong-h.github.io/_posts/2021-07-11-test-me.md

➜  mincong-h.github.io git:(permalink % u=) ✗ curl -I localhost:4000/en/test-me/
HTTP/1.1 200 OK
Etag: 300810333-a3b7-60eb1d83
Content-Type: text/html; charset=utf-8
Content-Length: 41911
Last-Modified: Sun, 11 Jul 2021 16:34:11 GMT
Cache-Control: private, max-age=0, proxy-revalidate, no-store, no-cache, must-revalidate
Server: WEBrick/1.4.2 (Ruby/2.6.3/2019-04-16)
Date: Sun, 11 Jul 2021 16:35:02 GMT
Connection: Keep-Alive

Existing posts remain unchanged:

➜  mincong-h.github.io git:(permalink % u=) ✗ curl -I localhost:4000/2021/04/20/an-important-change/
HTTP/1.1 200 OK
Etag: 3007ec374-a679-60eb1d83
Content-Type: text/html; charset=utf-8
Content-Length: 42617
Last-Modified: Sun, 11 Jul 2021 16:34:11 GMT
Cache-Control: private, max-age=0, proxy-revalidate, no-store, no-cache, must-revalidate
Server: WEBrick/1.4.2 (Ruby/2.6.3/2019-04-16)
Date: Sun, 11 Jul 2021 16:35:59 GMT
Connection: Keep-Alive

Script:

```
paths=($(find "${HOME}/github/mincong-h.github.io/_posts" -type f -name "*.md" | tr '\n' ' '))
i=0
for path in "${paths[@]}"
do
    filename="${path##*/}"
    year=$(echo $filename  | sed -E 's/^([[:digit:]]+)-([[:digit:]]+)-([[:digit:]]+)-(.*)\.md/\1/')
    month=$(echo $filename | sed -E 's/^([[:digit:]]+)-([[:digit:]]+)-([[:digit:]]+)-(.*)\.md/\2/')
    day=$(echo $filename   | sed -E 's/^([[:digit:]]+)-([[:digit:]]+)-([[:digit:]]+)-(.*)\.md/\3/')
    name=$(echo $filename  | sed -E 's/^([[:digit:]]+)-([[:digit:]]+)-([[:digit:]]+)-(.*)\.md/\4/')
    permalink="/${year}/${month}/${day}/${name}/"
    echo "${i}: year=${year}, month=${month}, day=${day}, name=${name}, permalink=${permalink}"
    sed -i '' -E '/comments:/i\
permalink:         PERMALINK
' "$path"
    sed -i '' "s|PERMALINK|${permalink}|" "$path"
    i=$((i + 1))
done
```
@mincong-h mincong-h merged commit 395fccf into master Jul 11, 2021
@mincong-h mincong-h deleted the permalink branch July 11, 2021 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant