Skip to content

Instantly share code, notes, and snippets.

@bblanchon
Created February 28, 2020 16:20
Show Gist options
  • Save bblanchon/70826702b4b6c060008dd476fc2d9f84 to your computer and use it in GitHub Desktop.
Save bblanchon/70826702b4b6c060008dd476fc2d9f84 to your computer and use it in GitHub Desktop.
Add redirect_from to all Jekyll posts
from pathlib import Path
for file in Path('_posts').glob('*.md'):
print(file.name)
year = file.name[0:4]
month = file.name[5:7]
day = file.name[8:10]
title = file.name[11:-3]
oldurl = f'/{year}/{month}/{day}/{title}/'
print(oldurl)
content = file.read_text(encoding='utf-8').replace('---\ntitle:', f'---\nredirect_from: {oldurl}\ntitle:')
file.write_text(content, encoding='utf-8')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment