Skip to content

Commit

Permalink
Merge pull request #3 from leafney/t1122
Browse files Browse the repository at this point in the history
T1122
  • Loading branch information
leafney authored Dec 16, 2020
2 parents 47826d9 + bcdb8ce commit c54be1a
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 32 deletions.
Binary file removed Markdown Search.alfredworkflow
Binary file not shown.
59 changes: 53 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,67 @@
# alfred-markdown-search
Alfred Workflow for search and open markdown files

Alfred Workflow for search and open Markdown files

## Download
## Documents

Coming Soon...
* [English document](REAMDME.md)
* [中文文档](README_ZH.md)


## Params
## Pain Point

* `MARKDOWN_PATH`
When using Typora to manage markdown documents, the built-in search function is not very useful. Some documents cannot be searched.


## Params Setting

Open the environment variable setting page of `Markdown Search workflow`, and configure:

* `MARKDOWN_PATH` -- `Required` -- Directory path of markdown files,multiple directories are separated by English colon `:`
* `MARKDOWN_APP` -- `Optional` -- Third-party apps, such as `/Applications/Typora.app`

## How to use

Keyword: `mds`
Keyword: `mdd` (you can also customize this shortcut to trigger workflow)


1. When no parameters are entered, 20 document records are displayed in reverse order according to the latest modification time;
2. Support the `-t` (`title`) parameter to search the title area of the document (`title area`: to support multiple md document formats, define the `first nine lines` of the document as the title area for keyword search) ;
3. Support inputting multiple keywords to filter, separated by spaces, keywords are not case sensitive;
4. Select the corresponding line and press `Enter` to open the document with the system default markdown document App;
5. Select the corresponding line and press `Options + Enter`, the document will be opened with a custom-configured third-party App;


## Examples

```
# By default, 20 document records are displayed according to the latest modification time
mdd
# Search keywords in the title area, the keywords are not case sensitive
mdd -t golang
# Full text search keywords, keywords are not case sensitive
mdd golang
# Full text search keywords, multiple keywords are separated by spaces, keywords are not case sensitive
mdd golang Python
# Find documents that contain `golang` in the title area and `python` in the content of the document
mdd -t golang python
# Find documents with `golang` and `python` in the title area
mdd -t golang,python
# Find documents that contain `golang` and `python` keywords in the title area and `mysql` in the document content
mdd -t golang,python mysql
```


## Thanks
Expand Down
68 changes: 68 additions & 0 deletions README_ZH.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# alfred-markdown-search

通过 Alfred Workflow 搜索和打开 Markdown 文档

## 文档

* [English document](REAMDME.md)
* [中文文档](README_ZH.md)


## 痛点

在使用 Typora 管理 Markdown 文档时,自带的搜索功能不是很好用,有些文档搜索不到。


## 参数设置

打开 `Markdown Search workflow` 的环境变量设置页面,配置如下环境变量:

* `MARKDOWN_PATH` -- `必填项` -- markdown文档所在目录路径,多个目录用英文冒号`:`分隔
* `MARKDOWN_APP` -- `可选项` -- 第三方App, 例如 `/Applications/Typora.app`

## 如何使用

关键词: `mdd` (也可以自定义该快捷键触发 workflow)

1. 不输入任何参数时,按照最近修改时间倒序展示20条文档记录;
2. 支持 `-t` (`title`)参数,对文档标题区域搜索(`标题区域`:为支持多种md文档格式,定义文档的 `前九行` 为标题区域来进行关键字查找);
3. 支持输入多个关键字过滤,中间以空格分隔,关键字不区分大小写;
4. 选中对应行,`回车`,会用系统默认的markdown文档App打开该文档;
5. 选中对应行,按下 `Options + 回车`,会用自定义配置的第三方App打开该文档;


## 示例

```
# 不输入任何参数,按最近修改时间展示20条文档记录
mdd
# 在标题区域搜索关键词,关键词不区分大小写
mdd -t golang
# 全文搜索关键词,关键词不区分大小写
mdd golang
# 全文搜索关键词,多个关键词之间用空格分隔,关键词不区分大小写
mdd golang Python
# 查找标题区域含有 golang ,且文档内容中包含 python 的文档
mdd -t golang python
# 查找标题区域含有 golang 和 python 的文档
mdd -t golang,python
# 查找标题区域含有 golang 和 python 关键词,且文档内容中包含 mysql 的文档
mdd -t golang,python mysql
```


## 感谢

* [MWeb workflow](https://github.com/tianhao/alfred-mweb-workflow)
16 changes: 12 additions & 4 deletions shell/md-search.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,26 @@ fi
final_expr="${final_expr} | head -20" # 取前20个


echo "{\"items\":["
result="{\"items\":["
n=0
r=0
while read line
do
# echo line=${line}

# 将文件名作为结果列表中的标题显示
h="${line##*/}"
echo "{\"type\": \"file\",\"title\": \"${h}\",\"subtitle\": \"${line}\",\"arg\": \"${line}\"},"
result+="{\"type\": \"file\",\"title\": \"${h}\",\"subtitle\": \"${line}\",\"arg\": \"${line}\"},"
n=$((n+1))
done < <(eval "${final_expr}")
if [ ${n} -eq 0 ]; then
echo "{\"title\": \"没有找到符合条件的文档\"}"
result+="{\"title\": \"没有找到符合条件的文档\"}"
fi
echo "]}"

# Remove the json str last comma
if [ ${r} -eq 0 ]; then
result=${result%,}
fi

result+="]}"
echo ${result}
69 changes: 47 additions & 22 deletions shell/test.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#!/usr/bin/env bash

# MARKDOWN_PATH="${HOME}/demo"
# NOTE:
# debug mode true or false
debug=false

if [ "$debug" = true ]; then
MARKDOWN_PATH="${HOME}/demo/mnk"
fi

if [ -z "${MARKDOWN_PATH}" ];then
echo "{ \"items\":[{\"type\": \"error\",\"title\": \"请先设置文档路径 MARKDOWN_PATH \"}]}"
exit 1
fi

# echo "${MARKDOWN_PATH}"

if [ "$debug" = true ]; then
echo "13--${MARKDOWN_PATH}"
fi

declare -a header_arr # 标题数组
declare -a keyword_arr # 关键字数组
Expand Down Expand Up @@ -48,15 +53,19 @@ get_params $*
final_expr=""
IFS=":";for i in ${MARKDOWN_PATH}
do
final_expr="${final_expr} find \"${i}\" -type f -iname '*.md';"
final_expr="${final_expr} find \"${i}\" -type f -iname '*.md' -print0;"
done
unset IFS
final_expr="{${final_expr}}"
#final_expr="{${final_expr}} | xargs -I'{}' stat -f'%m %N' '{}' | sort -rn | cut -d' ' -f 2-" # | xargs -I'{}' grep -l tomcat '{}"

# echo "${final_expr}"
# echo "header_arr:${header_arr} -- keyword_arr:${keyword_arr} -- next_input:${next_input} -- last_input:${last_input} -- end_char:${end_char}"
if [ "$debug" = true ]; then
echo "60--${final_expr}"
echo "header_arr:[${header_arr}] -- keyword_arr:[${keyword_arr}] -- next_input:[${next_input}] -- last_input:[${last_input}] -- end_char:[${end_char}]"
fi

# 按时间排序
final_expr="${final_expr} | xargs -0 ls -t"

# 如果有输入-h参数,过滤文档标题
# 思路: grep -n 会输出行号,找到符合所有关键字的行,筛选行号=1的文档就可以了
Expand All @@ -70,9 +79,9 @@ if [ ${#header_arr[@]} -gt 0 ];then
final_expr="${final_expr} | egrep '^.+\.md\:[1-9]\:' | awk -F':' '{print \$1}'"
fi

# echo "${final_expr}"
# ➜ { find "/demo" -type f -iname '*.md';} | xargs -I'{}' grep -inHe 'aaa' '{}' | egrep '^.+\.md\:[1-9]\:' | awk -F':' '{print $1}'

if [ "$debug" = true ]; then
echo "78--${final_expr}"
fi

# 如果有输入关键字,则用关键字筛选文档,并且按照文档标题匹配度排序
if [ ${#keyword_arr[@]} -gt 0 ];then
Expand All @@ -81,11 +90,13 @@ if [ ${#keyword_arr[@]} -gt 0 ];then
final_expr="${final_expr} | xargs -I'{}' grep -ile '${i}' '{}' | awk -F':' '{print \$1}' | uniq "
done

# echo "83--${final_expr}"
if [ "$debug" = true ]; then
echo "89--${final_expr}"
fi

# 按时间排序
# final_expr="${final_expr} | xargs -I'{}' stat -f'%m %N' '{}' | sort -rn | cut -d' ' -f 2- "
final_expr="${final_expr} | xargs -I'{}' ls -t '{}'"
# final_expr="${final_expr} | xargs -I'{}' ls -t '{}'"

# 排序表达式: 统计第一行匹配关键字个数,将匹配个数大的放在前面
# 第一步: 输入"文件名",输出"文件名 匹配个数"
Expand All @@ -95,32 +106,46 @@ if [ ${#keyword_arr[@]} -gt 0 ];then
egrep_expr=$(echo "${keyword_arr[@]}" | sed "s/[[:blank:]]/|/g")
sort_expr="awk -F'\\n' '{system(\"egrep -ioe \\\"${egrep_expr}\\\" <<< \`head -1 \\\"\"\$1 \"\\\"\` | wc -l | xargs -I\\\"{}\\\" echo \\\"{}\\\" \\\"\"\$1\"\\\"\")}' | sort -r | cut -d' ' -f 2-"
final_expr="${final_expr} | ${sort_expr} "

if [ "$debug" = true ]; then
echo "106--${final_expr}"
fi
else
# 按时间排序
# final_expr="${final_expr} | xargs -I'{}' stat -f'%m %N' '{}' | sort -rn | cut -d' ' -f 2- "
final_expr="${final_expr} | xargs -I'{}' ls -t '{}'"
# final_expr="${final_expr} | xargs -0 ls -t"

if [ "$debug" = true ]; then
echo "114--${final_expr}"
fi
fi

final_expr="${final_expr} | head -20" # 取前20个

if [ "$debug" = true ]; then
echo "121--${final_expr}"
fi

# echo "104--${final_expr}"


echo "{\"items\":["
result="{\"items\":["
n=0
r=0
while read line
do
# echo line=${line}

# 将文件中的前两行作为结果列表中的标题显示
# h="$(head -2 "${line}"| sed 's/-//g;s/\\/\\\\/g;s/"/\\"/g;s/[[:space:]]*$//g')"
# 将文件名作为结果列表中的标题显示
h="${line##*/}"
echo "{\"type\": \"file\",\"title\": \"${h}\",\"subtitle\": \"${line}\",\"arg\": \"${line}\"},"
result+="{\"type\": \"file\",\"title\": \"${h}\",\"subtitle\": \"${line}\",\"arg\": \"${line}\"},"
n=$((n+1))
done < <(eval "${final_expr}")
if [ ${n} -eq 0 ]; then
echo "{\"title\": \"没有找到符合条件的文档\"}"
result+="{\"title\": \"没有找到符合条件的文档\"}"
fi
echo "]}"

# Remove the json str last comma
if [ ${r} -eq 0 ]; then
result=${result%,}
fi

result+="]}"
echo ${result}

0 comments on commit c54be1a

Please sign in to comment.