Skip to content

Commit

Permalink
Change how the language is encoded in the URL
Browse files Browse the repository at this point in the history
  • Loading branch information
bokub committed Apr 28, 2020
1 parent 9069e46 commit af22dc4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ <h1 class="title my-0">{ NoPaste }</h1>
<div id="editor"></div>
<footer id="footer" class="shadow-top container-fluid">
<div class="row my-1">
<div class="col mono hide-readonly" id="stats"></div>
<div class="col mono hide-readonly" id="stats">Length: 0 | Lines: 1</div>
<div class="col mono show-readonly">Hosted on NoPaste.ml</div>
<div class="col-auto mono show-readonly">
<a href="javascript:void(0)" onclick="openInNewTab()">
Expand Down
56 changes: 51 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const initCodeEditor = () => {
statsEl = byId('stats');
editor.on('change', () => {
statsEl.innerHTML = `Length: ${editor.getValue().length} | Lines: ${editor['doc'].size}`;
hideCopyBar();
});
};

Expand All @@ -37,7 +38,7 @@ const initLangSelector = () => {
select: '#language',
data: CodeMirror.modeInfo.map((e) => ({
text: e.name,
value: slugify(e.name),
value: hash(e.name),
data: { mime: e.mime, mode: e.mode },
})),
showContent: 'down',
Expand All @@ -48,11 +49,24 @@ const initLangSelector = () => {
},
});

select.set(decodeURIComponent(new URLSearchParams(window.location.search).get('lang') || 'plain-text'));
// Retro-compatibility
const legacyLang = new URLSearchParams(window.location.search).get('lang');
if (legacyLang) {
const langObj = CodeMirror.modeInfo.find((e) => slugify(e.name) === legacyLang);
if (langObj) {
select.set(hash(langObj.name));
return;
}
}
// Set lang selector
select.set(window.location.hash.charAt(5) === '-' ? window.location.hash.substr(1, 4) : hash('Plain Text'));
};

const initCode = () => {
const base64 = location.pathname.substr(1) || location.hash.substr(1);
let base64 = location.pathname.substr(1) || location.hash.substr(1);
if (base64.charAt(4) === '-') {
base64 = base64.substr(5);
}
if (base64.length === 0) {
return;
}
Expand Down Expand Up @@ -131,8 +145,8 @@ const openInNewTab = () => {
// Build a shareable URL
const buildUrl = (rawData, mode) => {
const base = `${location.protocol}//${location.host}/`;
const query = `?lang=${encodeURIComponent(select.selected())}`;
const url = base + query + '#' + rawData;
const lang = hash('Plain Text') === select.selected() ? '' : select.selected() + '-';
const url = base + '#' + lang + rawData;
if (mode === 'markdown') {
return `[NoPaste snippet](${url})`;
}
Expand Down Expand Up @@ -167,6 +181,10 @@ const decompress = (base64, cb) => {

// Transform a plain text string into a compressed base64 string
const compress = (str, cb) => {
if (str.length === 0) {
cb('');
return;
}
const progressBar = byId('progress');

lzma.compress(
Expand Down Expand Up @@ -202,6 +220,20 @@ const slugify = (str) =>

const byId = (id) => document.getElementById(id);

const hash = function (str, seed = 0) {
let h1 = 0xdeadbeef ^ seed;
let h2 = 0x41c6ce57 ^ seed;
for (let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i);
h1 = Math.imul(h1 ^ ch, 2654435761);
h2 = Math.imul(h2 ^ ch, 1597334677);
}
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
const h = 4294967296 * (2097151 & h2) + (h1 >>> 0);
return h.toString(36).substr(0, 4).toUpperCase();
};

/* Only for tests purposes */
const testAllModes = () => {
for (const [index, language] of Object.entries(CodeMirror.modeInfo)) {
Expand All @@ -212,4 +244,18 @@ const testAllModes = () => {
}
};

/* Only for tests purposes */
const testHashCollisions = () => {
const hashes = {};
console.time('Hash test');
for (const lang of CodeMirror.modeInfo) {
const hashed = hash(lang.name);
if (hashes[hashed]) {
console.error(`${lang.name} and ${hashes[hashed]} share the same hash: ${hashed}`);
}
hashes[hashed] = lang.name;
}
console.timeEnd('Hash test');
};

init();
3 changes: 2 additions & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ a:hover,
a:active {
color: #fff;
}
#controls a {
#controls a,
#footer a {
text-decoration: none;
}
.CodeMirror {
Expand Down

0 comments on commit af22dc4

Please sign in to comment.