DEV Community

Vuong
Vuong

Posted on

Reading Hacker News weekly posts with GitHub CLI

I used to subscribe to the Hacker News weekly email newsletter, but for some reason, I stopped receiving those emails.

Fortunately, I discovered the headllines/hackernews-weekly repository, which is active and regularly updated.

Using the GitHub mobile app, I can easily keep up with the latest Hacker News content. We could read it from terminal as well! Using GH CLI just cool-enough to help us for having that kind of experience.

Usage

Use gh to interact with GitHub from the command line

First, log in to GitHub CLI by running:

gh auth login
Enter fullscreen mode Exit fullscreen mode

To list available articles, use:

gh issue -R headllines/hackernews-weekly list
Enter fullscreen mode Exit fullscreen mode

To view a specific article, run:

gh issue -R headllines/hackernews-weekly view <issue_number>
Enter fullscreen mode Exit fullscreen mode

Enhancing reading experience

To shorten GitHub CLI commands, I use zsh-abbr:

abbr hn="gh issue -R headllines/hackernews-weekly"
Enter fullscreen mode Exit fullscreen mode

Now, running hn list will display the list of GitHub issues:
hn list

And hn view 288 will show issue #288, which contains the top 10 posts:
hn view 288

Sometimes, I would like to quickly fetch and read a website as markdown as well. I need to install some tools:

  • pandoc: Convert tool, we can use it to covert HTML content to markdown
  • glow: Render markdown files directly in the terminal

I have a small bash function to cache article content for offline reading or quick access later, add it into my ~/.zshrc:

function html2md() {
    cache_dir="$HOME/Downloads/html2md"
    cache_file="$cache_dir/$(echo -n "$1" | shasum | awk '{print $1}').md"
    mkdir -p "$cache_dir"

    if [ -f "$cache_file" ]; then
    else
        pandoc -f html -t gfm-raw_html "$1" | tee "$cache_file"
    fi

    glow "$cache_file"
}
Enter fullscreen mode Exit fullscreen mode

To read a specific link, simply run:

html2md https://fly.io/blog/youre-all-nuts/
Enter fullscreen mode Exit fullscreen mode

This will display:
html2md

Top comments (0)