Created
July 30, 2021 06:12
-
-
Save honmanyau/5680d1c7b823d454122a0275ba3fe60a to your computer and use it in GitHub Desktop.
Super Simple Offline Markdown Editor
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Super Simple Offline Markdown Editor</title> | |
<script src="./marked.min.js"></script> | |
</head> | |
<body> | |
<style> | |
:root { | |
--spacing: 20px; | |
--half-spacing: calc(var(--spacing) / 2); | |
--border: 1px solid #333; | |
} | |
* { | |
box-sizing: border-box; | |
} | |
body { | |
margin: 0; | |
padding: var(--spacing); | |
background: var(--neu-primary); | |
} | |
#markdown { | |
margin-right: var(--spacing); | |
} | |
#parsed { | |
margin-left: var(--spacing); | |
padding: 0 20px; | |
overflow: scroll; | |
} | |
#load { | |
display: none; | |
} | |
.container, | |
.controls { | |
display: flex; | |
margin: var(--spacing) auto; | |
width: 90vw; | |
max-width: 1280px; | |
} | |
.container { | |
justify-content: space-between; | |
} | |
.controls { | |
justify-content: flex-end; | |
} | |
.box { | |
flex-grow: 1; | |
padding: var(--spacing); | |
width: 45%; | |
min-height: 20rem; | |
border: var(--border); | |
} | |
.button { | |
margin-left: var(--half-spacing); | |
padding: 0.5em 1em; | |
font-size: 0.9rem; | |
text-decoration: none; | |
border: var(--border); | |
color: black; | |
background: transparent; | |
} | |
.button:hover { | |
cursor: pointer; | |
} | |
.button-load { | |
border-color: crimson; | |
color: white; | |
background: crimson; | |
} | |
</style> | |
<div class="controls"> | |
<a id="save" class="button" role="button" href="#"> | |
DOWNLOAD DATA | |
</a> | |
<label class="button button-load" role="button" for="load"> | |
LOAD | |
<input id="load" type="file" /> | |
</label> | |
</div> | |
<main class="container"> | |
<textarea id="markdown" class="box" cols="64"></textarea> | |
<div id="parsed" class="box"></div> | |
</main> | |
<script> | |
const SAVE_INTERVAL = 5000; | |
const STORAGE_KEY = 'markdown-editor'; | |
const markdown = document.getElementById('markdown'); | |
const parsed = document.getElementById('parsed'); | |
const saveButton = document.getElementById('save'); | |
const loadButton = document.getElementById('load'); | |
const restoreSession = new Promise((resolve) => { | |
markdown.value = localStorage.getItem('storageKey'); | |
parsed.innerHTML = marked(markdown.value); | |
resolve({ ok: true }); | |
}); | |
restoreSession | |
.then(() => { | |
try { | |
if (localStorage) { | |
setInterval(() => { | |
localStorage.setItem('storageKey', markdown.value); | |
}, SAVE_INTERVAL); | |
} | |
} catch { | |
console.error('localStorage unavailable. Please download your data!'); | |
} | |
}); | |
markdown.oninput = (e) => { | |
parsed.innerHTML = marked(e.currentTarget.value); | |
} | |
saveButton.onclick = (e) => { | |
e.currentTarget.href = `data:text/plain;base64,${btoa(markdown.value)}`; | |
e.currentTarget.download = `markdown-${Date.now()}.md`; | |
}; | |
loadButton.onchange = (e) => { | |
e.currentTarget.files[0].text() | |
.then((v) => { | |
markdown.value = v; | |
parsed.innerHTML = marked(markdown.value); | |
}); | |
}; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A visit down the rabbit hole due to a comment by usrme on this Hacker News post. Note that this was cobbled up as a toy, almost no effort was made to optimise anything and I shall not be liable for any loss of data.
Edit: Included warning about
localStorage
and auto-saving.Warning
The auto-saving feature depends on
localStorage
, which doesn't work in private browsing mode and its behaviour is not consistent across different browsers. Please do not rely on auto-saving and back up your data manually using the "Download Data" button where necessary!Instructions
.html
file.Reduce Processing
textarea.oninput = (e) => {
totextarea.onchange = (e) => {
. Do note that you will need to defocus the textarea in order for the update to occur!localStorage
every 5000 ms. To change this behaviour, please edit theSAVE_INTERVAL
constant.LICENSE
MIT License
Copyright (c) 2021 Honman Yau
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.