Test page: Added "Share" option (#2575) This adds a link and a small popup to share the current state of the test page via a compact link.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
diff --git a/.github/ISSUE_TEMPLATE/highlighting-bug-report.md b/.github/ISSUE_TEMPLATE/highlighting-bug-report.md
index 7ca00dc..1e8c5e6 100644
--- a/.github/ISSUE_TEMPLATE/highlighting-bug-report.md
+++ b/.github/ISSUE_TEMPLATE/highlighting-bug-report.md
@@ -13,8 +13,9 @@ assignees: ''
- Plugins: [a list of plugins you are using or 'none']
<!--
-Does the problem still occur in the latest version of Prism?
-You can check using the [test page](https://prismjs.com/test.html) or get the latest version at the [download page](https://prismjs.com/download.html).
+Please verify that the problem still occurs in the latest version of Prism.
+
+You can check this using the [test page](https://prismjs.com/test.html) or by getting the latest version at our [download page](https://prismjs.com/download.html).
-->
**Description**
@@ -22,6 +23,11 @@ A clear and concise description of what is being highlighted incorrectly and how
**Code snippet**
+<!--
+Please add a link to the [test page](https://prismjs.com/test.html) that reproduces your issue (hover over "Share" and insert the link below).
+-->
+[Test page]()
+
<details>
<summary>The code being highlighted incorrectly.</summary>
diff --git a/test.html b/test.html
index 55caccf..1c2aef4 100644
--- a/test.html
+++ b/test.html
@@ -91,6 +91,44 @@ pre.show-tokens {
margin: 0 1px;
}
+#options {
+ position: relative;
+}
+
+.share-wrapper {
+ position: absolute;
+ top: 0;
+ right: 0;
+ background-color: white;
+}
+.share-wrapper .hidden-wrapper {
+ display: none;
+}
+
+.share-wrapper:hover {
+ top: -.5em;
+ right: -1em;
+ width: 300px;
+ padding: .5em 1em;
+ outline: 1px solid #888;
+}
+.share-wrapper:hover .hidden-wrapper {
+ display: block;
+}
+
+.share-wrapper input {
+ width: 100%;
+ box-sizing: border-box;
+}
+
+.share-wrapper button {
+ border: none;
+ background: none;
+ font: inherit;
+ text-decoration: underline;
+ cursor: pointer;
+}
+
</style>
<script src="assets/prefixfree.min.js"></script>
@@ -115,9 +153,16 @@ pre.show-tokens {
<p>Result:</p>
<pre><code></code></pre>
- <p id="options">
+ <div id="options" style="margin: 1em 0">
<label><input type="checkbox" id="option-show-tokens"> Show tokens</label>
- </p>
+ <div class="share-wrapper">
+ <a id="share-link" href="" style="float: right;">Share</a>
+ <div class="hidden-wrapper">
+ <input id="share-link-input" type="text" readonly onClick="this.select();"/>
+ <button type="button" id="copy-share-link" onclick="copyShare();">Copy to clipboard</button>
+ </div>
+ </div>
+ </div>
<p id="language">
<strong>Language:</strong>
</p>
@@ -151,18 +196,41 @@ function highlightCode() {
code = newCode;
};
-
+function getHashParams() {
+ return parseUrlParams((location.hash || '').substr(1));
+}
+function setHashParams(params) {
+ location.hash = stringifyUrlParams(params);
+}
function updateHashLanguage(lang) {
- location.hash = lang ? 'language=' + lang : '';
+ var params = getHashParams();
+ params.language = lang;
+ setHashParams(params);
}
function getHashLanguage() {
- var match = /[#&]language=([^&]+)/.exec(location.hash);
- return match ? match[1] : null;
+ return getHashParams().language;
}
function getRadio(lang) {
return $('input[name=language][value="' + lang + '"]');
}
+function copyShare() {
+ const link = $('#share-link').href;
+ try {
+ navigator.clipboard.writeText(link).then(function () {
+ $('#copy-share-link').textContent = 'Copied!';
+ }, function () {
+ $('#copy-share-link').textContent = 'Failed to copy!';
+ });
+ } catch (e) {
+ $('#copy-share-link').textContent = 'Failed to copy!';
+ }
+ setTimeout(function () {
+ $('#copy-share-link').textContent = 'Copy to clipboard';
+ }, 5000);
+}
+window.copyShare = copyShare;
+
window.onhashchange = function () {
var input = getRadio(getHashLanguage());
@@ -196,6 +264,7 @@ for (var id in languages) {
code.className = 'language-' + lang;
code.textContent = code.textContent;
updateHashLanguage(lang);
+ updateShareLink();
// loadLanguage() returns a promise, so we use highlightCode()
// as resolve callback. The promise will be immediately
@@ -277,9 +346,14 @@ selectedRadio.onclick();
var textarea = $('textarea', form);
try {
- var lastCode = sessionStorage.getItem('test-code');
- if (lastCode) {
- textarea.value = lastCode;
+ var hashText = getHashParams().text;
+ if (hashText) {
+ textarea.value = hashText;
+ } else {
+ var lastCode = sessionStorage.getItem('test-code');
+ if (lastCode) {
+ textarea.value = lastCode;
+ }
}
} catch (e) {
// ignore sessionStorage errors
@@ -290,6 +364,8 @@ textarea.oninput = function() {
code.textContent = codeText;
highlightCode();
+ updateShareLink();
+
try {
sessionStorage.setItem('test-code', codeText);
} catch (error) {
@@ -308,8 +384,57 @@ $('#option-show-tokens').onchange = function () {
};
$('#option-show-tokens').onchange();
-})();
+function updateShareLink() {
+ var params = {
+ language: /\blang(?:uage)?-([\w-]+)\b/i.exec(code.className)[1],
+ text: code.textContent,
+ };
+ $('#share-link').href = '#' + stringifyUrlParams(params);
+ $('#share-link-input').value = $('#share-link').href;
+}
+
+
+/**
+ * @param {Record<string, string | number | boolean>} params
+ * @returns {string}
+ */
+function stringifyUrlParams(params) {
+ var parts = [];
+ for (var name in params) {
+ if (params.hasOwnProperty(name)) {
+ var value = params[name];
+ if (typeof value === 'boolean') {
+ if (value) {
+ parts.push(name);
+ }
+ } else {
+ parts.push(name + '=' + encodeURIComponent(value));
+ }
+ }
+ }
+ return parts.join('&');
+}
+/**
+ * @param {string} str
+ * @returns {Record<string, string | boolean>}
+ */
+function parseUrlParams(str) {
+ /** @type {Record<string, string | boolean>} */
+ var params = {};
+ str.split(/&/g).filter(Boolean).forEach(function (part) {
+ var parts = part.split(/=/);
+ var name = parts[0];
+ if (parts.length === 1) {
+ params[name] = true;
+ } else {
+ params[name] = decodeURIComponent(parts[1]);
+ }
+ });
+ return params;
+}
+
+})();
</script>
</body>