Last active
May 4, 2022 11:57
-
-
Save qbosen/dd6d66255cd225e3daa7f3563737a01c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SCRIPT_DIR="$(cd $(dirname "$0") && pwd -P)" | |
LOGSEQ_DIR="$(dirname "$SCRIPT_DIR")" | |
PY_SCRIPT="clear_unref_images.py" | |
"$SCRIPT_DIR/$PY_SCRIPT" "$LOGSEQ_DIR" -b | |
echo 'DONE' | |
read |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import os | |
import re | |
from pathlib import Path | |
md_img_pattern = re.compile('!\[[^\]]*\]\((.+\.(?:jpg|jpeg|png|gif))\)') | |
def search_images_in_assets(content): | |
def path_relative_to_assets(): | |
for link in re.findall(md_img_pattern, content): | |
if link.find('assets/') >= 0: | |
yield link[link.find('assets/') + len('assets/'):] | |
return [path for path in path_relative_to_assets()] | |
parser = argparse.ArgumentParser(description='找出 logseq 中未引用的图片') | |
parser.add_argument('-b', '--bak', action='store_true', help='备份图片到 assets/bak') | |
parser.add_argument('-d', '--delete', action='store_true', help='删除图片,有备份选项时忽略') | |
parser.add_argument('workdir', type=str, action='store', nargs=1, help='logseq 根目录') | |
if __name__ == '__main__': | |
args = parser.parse_args() | |
# print(args) | |
workdir = Path(args.workdir[0]) | |
assets_dir = workdir / 'assets' | |
bak_dir = assets_dir / 'bak' | |
search_dirs = [workdir / 'pages', workdir / 'journals'] | |
images_in_notes = set() | |
for search_dir in search_dirs: | |
for root, dirs, files in os.walk(search_dir): | |
for file in files: | |
if file.endswith('.md'): | |
with open(Path(root, file)) as f: | |
images_in_notes.update(search_images_in_assets(f.read())) | |
# print(images_in_notes) | |
none_refs_images = set() | |
for root, dirs, files in os.walk(assets_dir): | |
for file in files: | |
if file.endswith(('jpg', 'jpeg', 'png', 'gif')): | |
image_relative_path = str(Path(root, file).relative_to(assets_dir)) | |
if image_relative_path not in images_in_notes: | |
none_refs_images.add(image_relative_path) | |
if not (args.bak or args.delete): | |
print('应该被删除的图片') | |
print(none_refs_images) | |
if args.bak: | |
if not bak_dir.exists(): | |
bak_dir.mkdir() | |
for images in none_refs_images: | |
img_file = Path(assets_dir, images) | |
if args.bak: | |
print('备份:\t', img_file) | |
img_file.rename(bak_dir / img_file.name) | |
elif args.delete: | |
print('删除:\t', img_file) | |
os.remove(img_file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SCRIPT_DIR="$(cd $(dirname "$0") && pwd -P)" | |
LOGSEQ_DIR="$(dirname "$SCRIPT_DIR")" | |
PY_SCRIPT="clear_unref_images.py" | |
"$SCRIPT_DIR/$PY_SCRIPT" "$LOGSEQ_DIR" -d | |
echo 'DONE' | |
read |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment