pantsdown 1.6.0 → 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.0",
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/lexer.ts CHANGED
@@ -57,7 +57,7 @@ export class Lexer {
57
57
  const tokenLines = rawToken.split("\n");
58
58
  const sourceMap: SourceMap = [this.line, (this.line += tokenLines.length - 1)];
59
59
 
60
- while (tokenLines.slice(-1)[0] === "") {
60
+ while (tokenLines[tokenLines.length - 1] === "") {
61
61
  // token.raw sometimes includes newline chars at the end of the string
62
62
  // which we don't want to include in the sourceMap
63
63
  tokenLines.pop();
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
  }
package/src/tokenizer.ts CHANGED
@@ -283,19 +283,25 @@ export class Tokenizer {
283
283
  loose: false,
284
284
  text: itemContents,
285
285
  tokens: [],
286
+ sourceMap: this.lexer.getSourceMap(raw),
286
287
  });
287
288
 
288
289
  list.raw += raw;
289
290
  }
290
291
 
291
292
  // Do not consume newlines at end of final item. Alternatively, make itemRegex *start* with any newlines to simplify/speed up endsWithBlankLine logic
292
- list.items[list.items.length - 1]!.raw = raw.trimEnd();
293
+ const lastTrimmed = raw.trimEnd();
294
+
295
+ if (list.items[list.items.length - 1]!.sourceMap) {
296
+ this.lexer.line -= raw.length - lastTrimmed.length;
297
+ }
298
+
299
+ list.items[list.items.length - 1]!.raw = lastTrimmed;
293
300
  list.items[list.items.length - 1]!.text = itemContents.trimEnd();
294
301
  list.raw = list.raw.trimEnd();
295
302
 
296
303
  // Item child tokens handled here at end because we needed to have the final item to trim it first
297
304
  for (let i = 0, listItemsLen = list.items.length; i < listItemsLen; i++) {
298
- list.items[i]!.sourceMap = this.lexer.getSourceMap(list.items[i]!.raw);
299
305
  this.lexer.state.top = false;
300
306
  list.items[i]!.tokens = this.lexer.blockTokens(list.items[i]!.text, []);
301
307
 
package/src/types.ts CHANGED
@@ -72,7 +72,7 @@ export interface Tokens extends Record<string, BaseToken> {
72
72
  checked?: boolean | undefined;
73
73
  loose: boolean;
74
74
  tokens: Token[];
75
- sourceMap?: SourceMap;
75
+ sourceMap: SourceMap;
76
76
  };
77
77
  Paragraph: {
78
78
  type: "paragraph";
package/bunfig.toml DELETED
@@ -1,2 +0,0 @@
1
- [test]
2
- preload = "./tests/happydom.ts"