pantsdown 1.6.1 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -131,5 +131,4 @@ console.log(html);
131
131
 
132
132
  ## Roadmap
133
133
 
134
- - [ ] Add button to copy code in fences
135
- - [ ] [Mermaid](https://mermaid.js.org/) usage docs
134
+ - [ ] [Mermaid](https://mermaid.js.org/) usage docs.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pantsdown",
3
- "version": "1.6.1",
3
+ "version": "1.7.1",
4
4
  "author": "wallpants",
5
5
  "description": "Markdown to 'GitHub HTML' parser",
6
6
  "license": "MIT",
@@ -0,0 +1,28 @@
1
+ export const codeCopyScript = `
2
+ <script id="code-copy-script" type="module">
3
+ document.querySelectorAll("pre").forEach((pre) => {
4
+ const copyButton = document.createElement("button");
5
+
6
+ const innerHTML =
7
+ '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="copy-base"><rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg>' +
8
+ '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="green" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" class="copy-success"><polyline points="20 6 9 17 4 12"/></svg>';
9
+
10
+ copyButton.innerHTML = innerHTML;
11
+ copyButton.className = "copy-button";
12
+
13
+ pre.appendChild(copyButton);
14
+ copyButton.addEventListener("click", () => {
15
+ navigator.clipboard.writeText(pre.firstChild.textContent)
16
+ .then(
17
+ () => {
18
+ copyButton.classList.add("success");
19
+ setTimeout(() => {
20
+ copyButton.classList.remove("success");
21
+ }, 1000);
22
+ }
23
+ );
24
+
25
+ });
26
+ });
27
+ </script>
28
+ `;
@@ -64,10 +64,42 @@
64
64
  word-wrap: break-word;
65
65
  }
66
66
 
67
+ .pantsdown .copy-button {
68
+ position: absolute;
69
+ left: calc(100% - 36px);
70
+ top: 5px;
71
+ cursor: pointer;
72
+ height: 30px;
73
+ width: 30px;
74
+ padding: 0;
75
+ background: var(--color-btn-bg);
76
+ border: 1px solid var(--color-btn-border);
77
+ border-radius: 4px;
78
+ display: flex;
79
+ align-items: center;
80
+ justify-content: center;
81
+ }
82
+ .pantsdown .copy-button.success {
83
+ border-color: green;
84
+ }
85
+ .pantsdown .copy-button:hover {
86
+ background: var(--color-btn-hover-bg);
87
+ }
88
+ .pantsdown .copy-button .copy-success {
89
+ display: none;
90
+ }
91
+ .pantsdown .copy-button.success .copy-success {
92
+ display: block;
93
+ }
94
+ .pantsdown .copy-button.success .copy-base {
95
+ display: none;
96
+ }
97
+
67
98
  .pantsdown .mermaid-container {
68
99
  margin-bottom: 16px;
69
100
  }
70
101
 
102
+ /* TODO: clean-up */
71
103
  /* .pantsdown .mermaid { */
72
104
  /* } */
73
105
 
package/src/pantsdown.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import GithubSlugger from "github-slugger";
2
+ import { codeCopyScript } from "./code-copy.ts";
2
3
  import { Lexer } from "./lexer.ts";
3
4
  import { Parser } from "./parser.ts";
4
5
  import { type PantsdownConfig } from "./types.ts";
@@ -18,6 +19,7 @@ export class Pantsdown {
18
19
  this.parser.renderer.slugger = new GithubSlugger();
19
20
 
20
21
  const tokens = this.lexer.lex(src);
21
- return this.parser.parse(tokens);
22
+ const html = this.parser.parse(tokens);
23
+ return codeCopyScript + html;
22
24
  }
23
25
  }
package/src/renderer.ts CHANGED
@@ -32,6 +32,7 @@ export class Renderer {
32
32
  const lang = (infostring ?? "").match(/^\S*/)?.[0];
33
33
  code = code.replace(/\n$/, "") + "\n";
34
34
 
35
+ // TODO: render pre and code instead of this section
35
36
  if (lang === "mermaid") {
36
37
  return injectHtmlAttributes(
37
38
  `<section><div class="mermaid">${code}</div></section>`,
@@ -44,11 +45,9 @@ export class Renderer {
44
45
  const highlightedCode = hljs.highlight(code, { language }).value;
45
46
 
46
47
  const result =
47
- `<pre><code class="hljs language-` +
48
- escape(language) +
49
- '">' +
50
- highlightedCode +
51
- "</code></pre>\n";
48
+ `<pre style="position: relative;">` +
49
+ `<code class="hljs language-${escape(language)}">${highlightedCode}</code>` +
50
+ `</pre>`;
52
51
 
53
52
  return injectHtmlAttributes(result, [], sourceMap);
54
53
  }