mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Add "Copy Link" link to demo, restore sample text from URL hash if present.
This commit is contained in:
parent
7aadb1124e
commit
827e1acb56
2 changed files with 29 additions and 0 deletions
|
|
@ -28,6 +28,8 @@
|
||||||
<div class="flex-columns inset">
|
<div class="flex-columns inset">
|
||||||
<footer><a href="https://github.com/DavidAnson/markdownlint">markdownlint project on GitHub</a></footer>
|
<footer><a href="https://github.com/DavidAnson/markdownlint">markdownlint project on GitHub</a></footer>
|
||||||
<div class="flex-fill"></div>
|
<div class="flex-fill"></div>
|
||||||
|
<footer><a href="#" id="copyLink">Copy Link</a></footer>
|
||||||
|
<div class="flex-fill"></div>
|
||||||
<footer>Copyright © 2015-2019 by <a href="https://dlaa.me/">David Anson</a></footer>
|
<footer>Copyright © 2015-2019 by <a href="https://dlaa.me/">David Anson</a></footer>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -8,13 +8,18 @@
|
||||||
var violations = document.getElementById("violations");
|
var violations = document.getElementById("violations");
|
||||||
var form = document.getElementsByTagName("form")[0];
|
var form = document.getElementsByTagName("form")[0];
|
||||||
var openFile = document.getElementById("openFile");
|
var openFile = document.getElementById("openFile");
|
||||||
|
var copyLink = document.getElementById("copyLink");
|
||||||
|
|
||||||
// Variables
|
// Variables
|
||||||
var markdownit = window.markdownit({ "html": true });
|
var markdownit = window.markdownit({ "html": true });
|
||||||
var newLineRe = /\r\n|\r|\n/;
|
var newLineRe = /\r\n|\r|\n/;
|
||||||
|
var hashPrefix = "%m";
|
||||||
var rulesMd = "https://github.com/DavidAnson/markdownlint" +
|
var rulesMd = "https://github.com/DavidAnson/markdownlint" +
|
||||||
"/blob/master/doc/Rules.md";
|
"/blob/master/doc/Rules.md";
|
||||||
|
|
||||||
|
// Do-nothing function
|
||||||
|
function noop() {}
|
||||||
|
|
||||||
// Sanitize string for HTML display
|
// Sanitize string for HTML display
|
||||||
function sanitize(str) {
|
function sanitize(str) {
|
||||||
return str
|
return str
|
||||||
|
|
@ -132,12 +137,22 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Updates the URL hash and copies the URL to the clipboard
|
||||||
|
function onCopyLinkClick(e) {
|
||||||
|
window.location.hash = encodeURIComponent(hashPrefix + markdown.value);
|
||||||
|
/* eslint-disable-next-line no-unused-expressions */
|
||||||
|
navigator.clipboard && navigator.clipboard.writeText &&
|
||||||
|
navigator.clipboard.writeText(window.location).then(noop, noop);
|
||||||
|
e.preventDefault();
|
||||||
|
}
|
||||||
|
|
||||||
// Add event listeners
|
// Add event listeners
|
||||||
document.body.addEventListener("dragover", onDragOver);
|
document.body.addEventListener("dragover", onDragOver);
|
||||||
document.body.addEventListener("drop", onDrop);
|
document.body.addEventListener("drop", onDrop);
|
||||||
openFile.addEventListener("change", onOpenFileChange);
|
openFile.addEventListener("change", onOpenFileChange);
|
||||||
markdown.addEventListener("input", onMarkdownInput);
|
markdown.addEventListener("input", onMarkdownInput);
|
||||||
violations.addEventListener("click", onLineNumberClick, true);
|
violations.addEventListener("click", onLineNumberClick, true);
|
||||||
|
copyLink.addEventListener("click", onCopyLinkClick);
|
||||||
|
|
||||||
/* eslint-disable max-len */
|
/* eslint-disable max-len */
|
||||||
markdown.value = [
|
markdown.value = [
|
||||||
|
|
@ -167,6 +182,18 @@
|
||||||
].join("\n");
|
].join("\n");
|
||||||
/* eslint-enable max-len */
|
/* eslint-enable max-len */
|
||||||
|
|
||||||
|
// Update Markdown from hash (if present)
|
||||||
|
if (window.location.hash) {
|
||||||
|
try {
|
||||||
|
const decodedHash = decodeURIComponent(window.location.hash.substring(1));
|
||||||
|
if (hashPrefix === decodedHash.substring(0, hashPrefix.length)) {
|
||||||
|
markdown.value = decodedHash.substring(hashPrefix.length);
|
||||||
|
}
|
||||||
|
} catch (ex) {
|
||||||
|
// Invalid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Detect legacy browsers
|
// Detect legacy browsers
|
||||||
try {
|
try {
|
||||||
/* eslint-disable-next-line no-new */
|
/* eslint-disable-next-line no-new */
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue