forked from thomiceli/opengist
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split hljs into a new file; improved dev vite server system (thomicel…
- Loading branch information
Showing
6 changed files
with
69 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import hljs from 'highlight.js'; | ||
import md from 'markdown-it'; | ||
|
||
document.querySelectorAll('.markdown').forEach((e: HTMLElement) => { | ||
e.innerHTML = md({ | ||
html: true, | ||
highlight: function (str, lang) { | ||
if (lang && hljs.getLanguage(lang)) { | ||
try { | ||
return '<pre class="hljs"><code>' + | ||
hljs.highlight(str, {language: lang, ignoreIllegals: true}).value + | ||
'</code></pre>'; | ||
} catch (__) { | ||
} | ||
} | ||
|
||
return '<pre class="hljs"><code>' + md().utils.escapeHtml(str) + '</code></pre>'; | ||
} | ||
}).render(e.textContent); | ||
}); | ||
|
||
document.querySelectorAll<HTMLElement>('.table-code').forEach((el) => { | ||
const ext = el.dataset.filename?.split('.').pop() || ''; | ||
|
||
if (hljs.autoDetection(ext) && ext !== 'txt') { | ||
el.querySelectorAll<HTMLElement>('td.line-code').forEach((ell) => { | ||
ell.classList.add('language-' + ext); | ||
hljs.highlightElement(ell); | ||
}); | ||
} | ||
|
||
el.addEventListener('click', event => { | ||
if (event.target && (event.target as HTMLElement).matches('.line-num')) { | ||
Array.from(document.querySelectorAll('.table-code .selected')).forEach((el) => el.classList.remove('selected')); | ||
|
||
const nextSibling = (event.target as HTMLElement).nextSibling; | ||
if (nextSibling instanceof HTMLElement) { | ||
nextSibling.classList.add('selected'); | ||
} | ||
|
||
|
||
const filename = el.dataset.filenameSlug; | ||
const line = (event.target as HTMLElement).textContent; | ||
const url = location.protocol + '//' + location.host + location.pathname; | ||
const hash = '#file-' + filename + '-' + line; | ||
window.history.pushState(null, null, url + hash); | ||
location.hash = hash; | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
</p> | ||
</div> | ||
|
||
<script type="module" src="{{ asset "hljs.ts" }}"></script> | ||
|
||
</div> | ||
</body> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters