Skip to content

Instantly share code, notes, and snippets.

@Tiryoh
Created June 15, 2021 15:45
Show Gist options
  • Save Tiryoh/41bccca5ef7f954ffed9f1829dc2dcc6 to your computer and use it in GitHub Desktop.
Save Tiryoh/41bccca5ef7f954ffed9f1829dc2dcc6 to your computer and use it in GitHub Desktop.
A shell script to add target="_blank" option to link in Markdown text
#!/usr/bin/env bash
set -eu
# (C) 2021 Daisuke Sato
# Released under the MIT License
# https://tiryoh.mit-license.org/2021
SRC_DIR=$(cd $(dirname ${BASH_SOURCE:-$0}); pwd)
# there is no Negative Lookahead in sed
# https
find . -name "*.md" | xargs sed -i -e 's#\([^\!]\)\[\(.*\)\](\(https://.*\))\([^\{]\)#\1[\2](\3){target=_blank rel=noopener}\4#g'
find . -name "*.md" | xargs sed -i -e 's#\([^\!]\)\[\(.*\)\](\(https://.*\))$#\1[\2](\3){target=_blank rel=noopener}#g'
find . -name "*.md" | xargs sed -i -e 's#^\[\(.*\)\](\(https://.*\))\([^\{]\)#[\1](\2){target=_blank rel=noopener}\3#g'
# http
find . -name "*.md" | xargs sed -i -e 's#\([^\!]\)\[\(.*\)\](\(http://.*\))\([^\{]\)#\1[\2](\3){target=_blank rel=noopener}\4#g'
find . -name "*.md" | xargs sed -i -e 's#\([^\!]\)\[\(.*\)\](\(http://.*\))$#\1[\2](\3){target=_blank rel=noopener}#g'
find . -name "*.md" | xargs sed -i -e 's#^\[\(.*\)\](\(http://.*\))\([^\{]\)#[\1](\2){target=_blank rel=noopener}\3#g'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment