Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple file extension mapping to highlighted lang #19

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

dimio
Copy link

@dimio dimio commented Jul 21, 2023

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
Copy link
Owner

bokub commented Jul 24, 2023

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/[email protected]/mode/meta.js

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

script.js Outdated
Comment on lines 61 to 63
fileExtToLang(l)
.then((lang) => select.set(decodeURIComponent(lang)))
.catch(select.set(shorten('Plain Text')));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this:

  • check for the ?l query parameter
  • if not found, check for a ?e that can contain the file extension
  • if not found, set to Plain Text

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your help. PR updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants