bokub / nopaste

:clipboard: Client-side paste service
https://nopaste.boris.sh
MIT License
372 stars 133 forks source link

Add simple file extension mapping to highlighted lang #19

Open dimio opened 1 year ago

dimio commented 1 year ago

Add a bash function for example:

nopaste() {
    [[ -n "${2}" ]] && NOPASTE_URL="${2}" || NOPASTE_URL='https://cp.dimio.org'
    FILE_EXT=$(printf '%s' "${1}" | awk -F . '{if (NF>1) {print $NF}}')
    cat "${1}" | xz --format=lzma | base64 -w0 | printf "%s/?l=%s#%s\n" "${NOPASTE_URL}" "${FILE_EXT}" "$(cat -)"
}

Make a test file: echo 'echo "Hello World" | tee ./helloworld.sh' | tee ./helloworld.sh Now use it like this: nopaste helloworld.sh - it's be highlighted as Shell

https://cp.dimio.org/?l=sh#XQAAgAD//////////wAymMlPivxG3YI4xcyNk1QKsBwb65mrsh4ppM+EJv4miAI/qa2MT8Mwb//8CoAA изображение

or nopaste helloworld.sh https://bokub.github.io/nopaste (no highlight right now)

https://bokub.github.io/nopaste/?l=sh#XQAAgAD//////////wAymMlPivxG3YI4xcyNk1QKsBwb65mrsh4ppM+EJv4miAI/qa2MT8Mwb//8CoAA

UPD: highlighted preview: https://deploy-preview-19--nopaste.netlify.app/?l=sh#XQAAgAD//////////wAymMlPivxG3YI4xcyNk1QKsBwb65mrsh4ppM+EJv4miAI/qa2MT8Mwb//8CoAA

bokub commented 1 year ago

Hi !

I think CodeMirror.modeInfo already contains all the information you need to map a file extension to a language:

https://cdn.jsdelivr.net/npm/codemirror@5.65.5/mode/meta.js

And as you can see, there is a CodeMirror.findModeByExtension function exported too.