#!/usr/bin/env bash
set -eo pipefail

if [[ "$1" == "-h" || "$1" == "--help" ]]; then
    cat <<EOF
Usage: $(basename "$0") [-h] <url>

Convert webpage URL to markdown using jina.ai.

Options:
    -h, --help    Show this help message

Arguments:
    url           URL of webpage to convert

EOF
    exit 0
fi

# More details: https://jina.ai/reader/
#TODO: проверить https://defuddle.md/

url="$1"; shift
curl "https://r.jina.ai/$url" \
    -sS \
    -H "DNT: 1" \
    -H "X-Md-Hr: ---" \
    -H "X-Base: final" \
    -H "X-Timeout: 10" \
    -H "X-Locale: ru-RU" \
    -H "X-No-Cache: true" \
    -H "X-Engine: browser" \
    -H "X-User-Agent: url2md" \
    -H "X-Md-Em-Delimiter: *" \
    -H "X-Md-Link-Style: inlined" \
    -H "X-Return-Format: markdown" \
    -H "X-Keep-Img-Data-Url: true" \
    -H "X-Md-Link-Reference-Style: collapsed" \
    "$@"
