pi-yank 0.1.0 → 0.1.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.
@@ -14,6 +14,23 @@
14
14
  * @returns {CodeBlock[]}
15
15
  */
16
16
  export function extractCodeBlocks(markdown) {
17
+ const topLevelBlocks = extractTopLevelCodeBlocks(markdown);
18
+ if (topLevelBlocks.length === 1) {
19
+ const [wrapper] = topLevelBlocks;
20
+ const innerBlocks = extractTopLevelCodeBlocks(wrapper.content);
21
+ if (innerBlocks.length > 1) {
22
+ return withPreviews(innerBlocks);
23
+ }
24
+ }
25
+
26
+ return withPreviews(topLevelBlocks);
27
+ }
28
+
29
+ /**
30
+ * @param {string} markdown
31
+ * @returns {CodeBlock[]}
32
+ */
33
+ function extractTopLevelCodeBlocks(markdown) {
17
34
  /** @type {CodeBlock[]} */
18
35
  const blocks = [];
19
36
  /** @type {OpenCodeBlock[]} */
@@ -59,9 +76,8 @@ export function extractCodeBlocks(markdown) {
59
76
 
60
77
  const finishedBlock = openBlocks.pop();
61
78
  if (finishedBlock && openBlocks.length === 0) {
62
- const content = finishedBlock.lines.join("\n");
63
79
  blocks.push({
64
- content,
80
+ content: finishedBlock.lines.join("\n"),
65
81
  preview: "",
66
82
  startLine: finishedBlock.startLine,
67
83
  infoString: finishedBlock.infoString,
@@ -82,16 +98,18 @@ export function extractCodeBlocks(markdown) {
82
98
  });
83
99
  }
84
100
 
85
- return blocks
86
- .sort((left, right) => left.startLine - right.startLine)
87
- .map((block, index) => ({
88
- ...block,
89
- preview: buildCodeBlockPreview(
90
- block.content,
91
- index + 1,
92
- block.infoString,
93
- ),
94
- }));
101
+ return blocks.sort((left, right) => left.startLine - right.startLine);
102
+ }
103
+
104
+ /**
105
+ * @param {CodeBlock[]} blocks
106
+ * @returns {CodeBlock[]}
107
+ */
108
+ function withPreviews(blocks) {
109
+ return blocks.map((block, index) => ({
110
+ ...block,
111
+ preview: buildCodeBlockPreview(block.content, index + 1, block.infoString),
112
+ }));
95
113
  }
96
114
 
97
115
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pi-yank",
3
3
  "private": false,
4
- "version": "0.1.0",
4
+ "version": "0.1.1",
5
5
  "type": "module",
6
6
  "description": "Lightweight /yank extension for pi that copies the last assistant message or a selected code block.",
7
7
  "keywords": [
@@ -5,7 +5,7 @@ import {
5
5
  extractCodeBlocks,
6
6
  } from "../extensions/yank-core.js";
7
7
 
8
- test("extracts a single outer markdown block when nested fences appear as literal examples", () => {
8
+ test("prefers inner sibling code blocks when a single outer markdown wrapper encloses them", () => {
9
9
  const text = `我来为你创建一个 Markdown 格式的测试文本:
10
10
 
11
11
  \`\`\`markdown
@@ -32,10 +32,11 @@ def hello():
32
32
  说明文字`;
33
33
 
34
34
  const blocks = extractCodeBlocks(text);
35
- assert.equal(blocks.length, 1);
35
+ assert.equal(blocks.length, 2);
36
36
  assert.match(blocks[0].content, /function hello\(\)/);
37
- assert.match(blocks[0].content, /def hello\(\):/);
38
- assert.equal(blocks[0].preview, "markdown: # Markdown 格式测试文档");
37
+ assert.match(blocks[1].content, /def hello\(\):/);
38
+ assert.equal(blocks[0].preview, "javascript: function hello() {");
39
+ assert.equal(blocks[1].preview, "python: def hello():");
39
40
  });
40
41
 
41
42
  test("extracts sibling top-level code blocks independently", () => {
@@ -59,6 +60,35 @@ after`;
59
60
  assert.equal(blocks[1].preview, 'python: print("b")');
60
61
  });
61
62
 
63
+ test("unwraps a single outer markdown wrapper around filename and script blocks", () => {
64
+ const text = `下面给你一套 pi / pi-mono 迁移的一键备份 +恢复脚本(macOS/Linux)。
65
+
66
+ 保存为:
67
+
68
+ \`\`\`bash
69
+ backup-pi.sh
70
+ \`\`\`
71
+
72
+ 内容如下:
73
+
74
+ \`\`\`bash
75
+ #!/usr/bin/env bash
76
+ set -euo pipefail
77
+ TIMESTAMP="$(date +%Y%m%d-%H%M%S)"
78
+ \`\`\``;
79
+
80
+ const wrapped = `\`\`\`markdown
81
+ ${text}
82
+ \`\`\``;
83
+ const blocks = extractCodeBlocks(wrapped);
84
+
85
+ assert.equal(blocks.length, 2);
86
+ assert.equal(blocks[0].content, "backup-pi.sh");
87
+ assert.match(blocks[1].content, /set -euo pipefail/);
88
+ assert.equal(blocks[0].preview, "bash: backup-pi.sh");
89
+ assert.equal(blocks[1].preview, "bash: #!/usr/bin/env bash");
90
+ });
91
+
62
92
  test("supports tilde fences", () => {
63
93
  const text = `~~~sql
64
94
  select 1;