pi-better-show 0.1.2 → 0.2.0

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
@@ -53,9 +53,12 @@ interface TextProfile {
53
53
  flavor: "discord" | "line" | "plain"; // Discord Markdown, or plain text
54
54
  width?: number; // columns a diagram may take; default 60
55
55
  mermaid?: (source: string, width: number) => string | undefined;
56
+ keep?: readonly BlockKind[]; // kinds emitted as their `raw` text instead of converted
56
57
  }
57
58
  ```
58
59
 
60
+ Every `BlockSegment` and `InvalidSegment` carries `raw`: the block's exact original text, fences (or an alert's whole `>` quote) included, byte for byte. Kinds listed in `keep` are emitted as `raw` verbatim, valid or invalid, for a surface that draws them itself; other kinds convert as usual. `DISPLAY_KINDS` (`alert`, `cards`, `mermaid`, `mindmap`, `diff`, `table`, `steps`, `stats`) is the usual `keep` list for such a surface. Without `keep`, output is unchanged.
61
+
59
62
  Renders a document for a surface that cannot draw blocks. A diagram goes through the `mermaid` hook; when there is no hook, or it returns `undefined` or a drawing wider than `width`, the output is the Mermaid source in a code block plus a pointer to the web view. No block-kind fence is left in the output, except Discord's native `diff`.
60
63
 
61
64
  ```ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-better-show",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "description": "Interactive blocks for agent-written Markdown: one vocabulary, text fallback, and answer encoding shared by every host, plus the show_doc extension for pi",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/core/parse.ts CHANGED
@@ -55,12 +55,17 @@ const CONVERTERS: { [K in Exclude<BlockKind, "alert">]: Convert<K> } = {
55
55
  const messageOf = (error: unknown): string =>
56
56
  ((error instanceof Error ? error.message : String(error)).split("\n")[0] as string).replace(/:$/, "");
57
57
 
58
- function convert(kind: Exclude<BlockKind, "alert">, source: string, index: number): BlockSegment | InvalidSegment {
58
+ function convert(
59
+ kind: Exclude<BlockKind, "alert">,
60
+ source: string,
61
+ index: number,
62
+ raw: string,
63
+ ): BlockSegment | InvalidSegment {
59
64
  try {
60
65
  const data = (CONVERTERS[kind] as Convert<BlockKind>)(source);
61
- return { type: "block", kind, data, index } as BlockSegment;
66
+ return { type: "block", kind, data, raw, index } as BlockSegment;
62
67
  } catch (error) {
63
- return { type: "invalid", kind, source, error: messageOf(error) };
68
+ return { type: "invalid", kind, source, raw, error: messageOf(error) };
64
69
  }
65
70
  }
66
71
 
@@ -121,7 +126,12 @@ export function parse(markdown: string): Segment[] {
121
126
  const kind = (info.trim().split(/\s+/)[0] as string).toLowerCase();
122
127
  if (fenceKinds.has(kind)) {
123
128
  flush();
124
- const segment = convert(kind as Exclude<BlockKind, "alert">, body.join("\n"), index);
129
+ const segment = convert(
130
+ kind as Exclude<BlockKind, "alert">,
131
+ body.join("\n"),
132
+ index,
133
+ lines.slice(i, stop).join("\n"),
134
+ );
125
135
  if (segment.type === "block") index++;
126
136
  segments.push(segment);
127
137
  } else {
@@ -138,7 +148,7 @@ export function parse(markdown: string): Segment[] {
138
148
  const data = alertOf(run);
139
149
  if (data) {
140
150
  flush();
141
- segments.push({ type: "block", kind: "alert", data, index: index++ });
151
+ segments.push({ type: "block", kind: "alert", data, raw: run.join("\n"), index: index++ });
142
152
  } else {
143
153
  prose.push(...run);
144
154
  }
package/src/core/text.ts CHANGED
@@ -272,8 +272,10 @@ export function toText(segments: Segment[], profile: TextProfile): string {
272
272
  width: profile.width ?? DEFAULT_WIDTH,
273
273
  mermaid: profile.mermaid,
274
274
  };
275
+ const keep = new Set<string>(profile.keep ?? []);
275
276
  return segments
276
277
  .map((segment) => {
278
+ if (segment.type !== "markdown" && keep.has(segment.kind)) return segment.raw;
277
279
  if (segment.type === "markdown") return dialect.markdown ? segment.text : plainProse(segment.text);
278
280
  if (segment.type === "invalid") return invalid(dialect, segment);
279
281
  return block(dialect, segment);
package/src/core/types.ts CHANGED
@@ -191,6 +191,8 @@ export type BlockSegment = {
191
191
  type: "block";
192
192
  kind: K;
193
193
  data: BlockDataMap[K];
194
+ /** The block's exact original text, fences (or an alert's whole `>` quote) included. */
195
+ raw: string;
194
196
  /** Counts the document's blocks of every kind from 0, in order; invalid blocks take no number. */
195
197
  index: number;
196
198
  };
@@ -200,6 +202,8 @@ export interface InvalidSegment {
200
202
  kind: BlockKind;
201
203
  /** The block's text between its fences (an alert's whole quote). */
202
204
  source: string;
205
+ /** The block's exact original text, fences (or an alert's whole `>` quote) included. */
206
+ raw: string;
203
207
  error: string;
204
208
  }
205
209
  export type Segment = MarkdownSegment | BlockSegment | InvalidSegment;
@@ -227,4 +231,9 @@ export interface TextProfile {
227
231
  width?: number;
228
232
  /** Draws a diagram as text, or returns undefined when it cannot (invalid, or wider than `width`). */
229
233
  mermaid?: (source: string, width: number) => string | undefined;
234
+ /** Kinds whose blocks (valid or invalid) are emitted as their `raw` text, for a surface that draws them itself. */
235
+ keep?: readonly BlockKind[];
230
236
  }
237
+
238
+ /** The kinds a display surface (a chat UI that draws blocks itself) typically keeps through `toText`. */
239
+ export const DISPLAY_KINDS = ["alert", "cards", "mermaid", "mindmap", "diff", "table", "steps", "stats"] as const;