How to find orphan pages in Gollum

200 Views Asked by At

In Gollum wiki, how do I find orphan pages - that is pages that don't have a link to them?

I have tried to write a bash script (see below) but it seems to not work good with pages that have special characters (, ), / in the name.

#!/bin/bash
found=""
find . -name HelpPages -prune -o -name '*.md' -type f -print0 | while IFS= read -r -d '' f; do
  escaped=$(echo $f | sed -e "s/.md//g")
  escaped=$(echo $escaped | sed -e "s/.\///g")
  escaped=$(echo $escaped | sed -e "s/(/\\\\(/g")
  escaped=$(echo $escaped | sed -e "s/)/\\\\)/g")
  escaped=$(echo $escaped | sed -e "s/\.\///g")
  escaped=$(echo $escaped | sed -E 's/[- ]/\[- \/\]/g')
  escaped=$(echo $escaped | sed -E 's/ /\\ /g')

  found=$(grep -i -r "[\|\[]$escaped\]" *.md)

  echo "_________"
  echo "Processing $escaped file..."
  if [ -n "$found" ]; then
    echo "found: $f"
    echo "$found"
  else
    echo "NOT found: $f"
  fi

done

This script first finds files within a directory that match *.md filename pattern. Then, for every filename, like The-event-log-(errors-warnings).md, the script needs to find the link, which can be any of the following:

[[The event log (errors/warnings)]]
[[The event log (errors warnings)]]
[[The-event-log-(errors-warnings)]]
[[The event log|The-event-log-(errors-warnings)]]
1

There are 1 best solutions below

0
On

Turned out I had some bad escaping of special characters. Ready script (also available in Gist):

#!/bin/bash
#Usage:
#./find-links.sh                    #shows linked pages and orphans
#./find-links.sh --html             #shows linked pages and orphans with HTML links
#./find-links.sh --md               #shows linked pages and orphans with Markdown links
#./find-links.sh --orphans          #shows only orphans
#./find-links.sh --orphans --html   #shows only orphans with HTML links
#./find-links.sh --linked           #shows only linked pages
#./find-links.sh --linked --html    #shows only linked pages with HTML links

outputOrphans=1
outputLinked=1
outputHtml=""
outputMarkdown=""

while test $# -gt 0
do
  case "$1" in
    --orphans) outputLinked=""
    ;;
    --linked) outputOrphans=""
    ;;
    --html) outputHtml=1
    ;;
    --md) outputMarkdown=1
  esac
  shift
done

found=""
find . -name HelpPages -prune -o -name '*.md' -type f -print0 | while IFS= read -r -d '' f; do
  escaped=$(echo $f | sed -e "s/.md//g")
  escaped=$(echo $escaped | sed -e "s/.\///g")
  escaped=$(echo $escaped | sed -e "s/(/\\\\(/g")
  escaped=$(echo $escaped | sed -e "s/)/\\\\)/g")
  escaped=$(echo $escaped | sed -e "s/\.\///g")
  escaped=$(echo $escaped | sed -E 's/[- ]/\[- \/\]/g')
  escaped=$(echo $escaped | sed -E 's/ /\\ /g')

  if [ -n "$outputHtml" ]; then
    linktext=$(echo $f | sed -e "s/.md//g")
    linktext=$(echo $linktext | sed -e "s/.\///g")
    linktext=$(echo $linktext | sed -E 's/[- ]/ /g')

    url=$(echo $f | sed -e "s/.md//g")
    url=$(echo $url | sed -e "s/.\///g")
    url=$(echo https://github.com/Starcounter/Starcounter/wiki/$url)

    output=$(echo "<a href='$url'>$linktext</a>")
  elif [ -n "$outputMarkdown" ]; then
      linktext=$(echo $f | sed -e "s/.md//g")
      linktext=$(echo $linktext | sed -e "s/.\///g")
      linktext=$(echo $linktext | sed -E 's/[- ]/ /g')

      url=$(echo $f | sed -e "s/.md//g")
      url=$(echo $url | sed -e "s/.\///g")
      url=$(echo https://github.com/Starcounter/Starcounter/wiki/$url)

      output=$(echo "- [$linktext]($url)")
  else
    output=$f
  fi

  found=$(grep -E -i -r "[\|\[]$escaped\]" *.md)

  if [ -n "$found" ]; then
    if [ -n "$outputLinked" ]; then
      echo $output
      found=$(echo "$found" | sed "s/^/    /g") #indent
      echo "$found"
    fi
  else
    if [ -n "$outputOrphans" ]; then
      echo $output
    fi
  fi

done