Skip to content

Instantly share code, notes, and snippets.

@legnoh
Last active May 17, 2020 07:22
Show Gist options
  • Save legnoh/c2223b6417c8debbf0e3a5cb8c6026a7 to your computer and use it in GitHub Desktop.
Save legnoh/c2223b6417c8debbf0e3a5cb8c6026a7 to your computer and use it in GitHub Desktop.
はてなブログをmdxに変換する時に使った雑なスクリプト群
#!/bin/bash
while read row; do
key=`echo ${row} | cut -d , -f 1`
title=`echo ${row} | cut -d , -f 5`
fulldatenum=`echo ${row} | cut -d , -f 4`
yearnum=`echo ${row} | cut -d , -f 6`
weeknum=`echo ${row} | cut -d , -f 7 | xargs printf %02d`
hatenaurl=`echo ${row} | cut -d , -f 2`
mkdir -p content/posts/201x/${yearnum}/${weeknum}/images
ln -fs ../../../../../../src/assets/no-hero.png ./content/posts/201x/${yearnum}/${weeknum}/images/no-hero.png
cat <<EOF >> content/posts/201x/${yearnum}/${weeknum}/index.mdx
---
title: ${title}
slug: ${yearnum}-${weeknum}
author: Ryoma Kai
date: ${fulldatenum}
hero: ./images/no-hero.png
excerpt:
$(cat ../../yhirano55/hatena-blog-exporter/post/${key}.md)
EOF
done < ~/Downloads/blog.csv
# 埋め込んだTwitterリンクをTwitter拡張に置換
egrep -r -l '\[https://twitter.com/.*/status/[0-9]+:embed#.*\]' ./** | xargs sed -i -e 's/\[\(https:\/\/twitter\.com\/.*\/status\/.*\):embed#.*\]/<Tweet tweetLink="\1" align="center" \/>/g'
# 画像表現を配下ディレクトリに変換
egrep -r -l '\[f:id:Ryo_K:([0-9]+).:.*\]' ./** | xargs gsed -i -e 's/\[f:id:Ryo_K:\([0-9]\+\).:plain]/![](.\/images\/\1.jpeg)/g'
# Amazon記法をURLに変換
egrep -r -l '\[asin:[A-Za-z0-9]+:detail]' ./** | xargs gsed -i -e 's/\[asin:\([A-Za-z0-9]\+\):detail]/- https:\/\/amazon.co.jp\/dp\/\1/g'
# embed:cite で埋め込まれたサイトをMarkdown記法に変換
egrep -r -l '\[(http(s)*://[0-9a-zA-Z_@=&%\+\?\/\.\-]+)(:title)*(:embed)*(:cite)*\]' ./** | xargs gsed -e "s/\[\(https*:\/\/[0-9a-zA-Z_@=&%\+\?\/\.\-]*\)[:title]*[:embed]*[:cite]*\]/\n- \1/g"
# バックアップファイルを削除
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment