hackmd-cli icon indicating copy to clipboard operation
hackmd-cli copied to clipboard

Feature Request: Filter List By Tag

Open krakov opened this issue 4 years ago • 1 comments

It would be very useful to use the list command to return only notes that match a tag.

Use case: use hackmd-cli to export some notes and batch commit them to git. Not every note should be exported, but because list returns all notes, need to download them all and then process them based on tag in YAML metadata. Listing by tag would reduce the number of exports speeding up the process.

krakov avatar Aug 08 '21 14:08 krakov

For reference, if useful for any purpose - this is the batch commit hackmd to git script, using hackmd-cli

#!/usr/bin/env bash

## Copies any hackmd file with a git: yaml metadata into its git location
## Use it for batch commit all changes in hackmd

## Installation: 
## npm install -g @hackmd/hackmd-cli
## hackmd-cli login 

TMP_SYNC_DIR=./.hackmd_sync/

function export_all {
    mkdir -p ${TMP_SYNC_DIR}
    rm -fr ${TMP_SYNC_DIR}/*
    cd ${TMP_SYNC_DIR}
    # would be nice to filter by tag here
    hackmd-cli list --output=csv --no-header --columns=id > all_notes
    for n in $(cat all_notes) ; do
        echo "Exporting ${n}" 
        hackmd-cli export --md $n > $n
    done
}

function update_file {
    f=$1
    # any note with a "git: <location>" yaml metadata will match
    git_name=$( awk 'NR==1{ if ($1!="---") exit 1 } /git:/ { print $2 } /---/ { if (NR>1) exit }' $f )
    [[ $? == 0 ]] || return
    diff -q $f ${git_name} > /dev/null
    [[ $? == 1 ]] || return
    cp -v $f ${git_name}
    # automatically prepare the commit
    # git add ${git_name}
}

function update_all {
    for f in ${TMP_SYNC_DIR}/* ; do
        update_file $f
    done
}

export_all
update_all

krakov avatar Aug 08 '21 14:08 krakov