sc0ttj / mdsh

A simple static site generator, using Markdown and Bash
https://sc0ttj.github.io/mdsh/
10 stars 0 forks source link

Add feature: permalinks #79

Closed sc0ttj closed 5 years ago

sc0ttj commented 5 years ago

Background

Form Jekyll site:

Permalinks are the output path for your pages, posts. They allow you to structure the directories of your source code different from the directories in your output.

For this project:

Permalinks are like URL shortcuts to longer, crappier URLs. You can define permalinks when creating or editing your posts. (Pages don't need permalinks as you can put them wherever you like already and manually create any extra symlinks you need).

How to:

GitHub Pages (at least) seems to support symlinks, so let's use those.

Allow easy adding of custom permalinks:

See: https://jekyllrb.com/docs/permalinks/

Also see ln usage:

# Create a symbolic link to a file or directory:

  ln -s path/to/file_or_directory path/to/symlink

# Overwrite an existing symbolic to point to a different file:

  ln -sf path/to/new_file path/to/symlink

So, something like:

# Function to be called from `create_page.sh`, where $post_date, 
# $post_url, $post_slug, etc, are already available:
function create_permalink {
  permalink="$1"
  # get post info for $2 (if given)
  if [ -f "${2//.html/.mdsh}" ];then
    get_post_info "${2//.html/.mdsh}"
  fi
  # delete symlinks to $post
  find -L . -xtype l -samefile posts/$post_date/$post_slug.html -delete
  # create permalink
  mkdir -p $(dirname $permalink)
  ln -s "$permalink"  "posts/$post_date/$post_slug.html"
  # update robots.txt: make search engines ignore full URL, 
  # and crawl/list only the permalink
  echo "Disallow: posts/$post_date/$post_slug.html" >> robots.txt
  sort -u robots.txt | uniq > cleaned-robots.txt
  mv cleaned-robots.txt robots.txt
}

Usage:

create_permalink "docs/liquid-filters" [<html-file>]

NOTE:

Also write a rm_permalink function which removes the symlink AND the relevant entry from robots.txt!

sc0ttj commented 5 years ago

Done in https://github.com/sc0ttj/mdsh/pull/85 , not including updating robots.txt