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
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ <h3>Because of this design:</h3>
</div>
</div>
</div>
<div id="resource-error-modal" class="modal" data-micromodal-close>
<div tabindex="-1" class="modal-overlay">
<div role="dialog" class="modal-content shadow-bottom p-3 m-3">
<div class="modal-close" data-micromodal-close><span class="icon-close"></span></div>
NoPaste cannot load resources<br /><br />
Sorry about that
</div>
</div>
</div>

<script src="https://cdn.jsdelivr.net/combine/
npm/[email protected]/src/lzma.min.js,
Expand Down
16 changes: 16 additions & 0 deletions resources/file-extension-to-lang.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"shll": [
"sh",
"bash",
"zsh"
],
"pwll": [
"ps1"
],
"perl": [
"pl"
],
"mrwn": [
"md"
]
}
35 changes: 34 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ let editor = null;
let select = null;
let clipboard = null;
let statsEl = null;
var fileExtToLangMap = null;

const init = () => {
Object.entries({
'fileExtToLangMap': 'file-extension-to-lang.json',
}).forEach(([varName, resource]) => this[varName] = loadResource(resource))
handleLegacyUrl();
initCodeEditor();
initLangSelector();
Expand Down Expand Up @@ -54,7 +58,9 @@ const initLangSelector = () => {

// Set lang selector
const l = new URLSearchParams(window.location.search).get('l');
select.set(l ? decodeURIComponent(l) : shorten('Plain Text'));
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.

};

const initCode = () => {
Expand Down Expand Up @@ -271,6 +277,33 @@ const hash = function (str, seed = 0) {
return h.toString(36).substr(0, 4).toUpperCase();
};

const fileExtToLang = (str) => {
return new Promise((resolve, reject) => {
if (str) {
fileExtToLangMap.then((mapping) => {
for (const [lang, fileExt] of Object.entries(mapping)) {
if (fileExt.includes(str)) {
resolve(lang);
return;
}
}
resolve(str);
});
} else {
reject(str);
}
})
}

const loadResource = (resource) => {
return fetch(`./resources/${resource}`)
.then(response => response.json())
.catch(error => {
console.log(error);
MicroModal.show('resource-error-modal');
});
}

// Only for tests purposes
const testAllModes = () => {
for (const [index, language] of Object.entries(CodeMirror.modeInfo)) {
Expand Down