pi-unsloth-webtools 0.2.0 → 0.2.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.
Files changed (2) hide show
  1. package/html-to-md.ts +31 -2
  2. package/package.json +1 -1
package/html-to-md.ts CHANGED
@@ -227,6 +227,21 @@ interface HtmlHandlers {
227
227
  const START_TAG_NAME_RE = /^[a-zA-Z][^\s/>]*/;
228
228
  const ATTR_NAME_RE = /^[^\s=/>]+/;
229
229
 
230
+ const RAW_TEXT_TAGS = [
231
+ "script",
232
+ "style",
233
+ "textarea",
234
+ "title",
235
+ "xmp",
236
+ "iframe",
237
+ "noembed",
238
+ "noframes",
239
+ ];
240
+
241
+ const RAW_TEXT_CLOSERS: Record<string, RegExp> = Object.fromEntries(
242
+ RAW_TEXT_TAGS.map((name) => [name, new RegExp(`</${name}\\s*>`, "i")]),
243
+ );
244
+
230
245
  function parseAttrsUntilClose(input: string, pos: number): [AttrDict, number, boolean] {
231
246
  const attrs: AttrDict = {};
232
247
  while (pos < input.length) {
@@ -346,8 +361,22 @@ export function feedHtml(input: string, handlers: HtmlHandlers): void {
346
361
  continue;
347
362
  }
348
363
  emitText(input.slice(textStart, i));
349
- if (tag.kind === "start") handlers.handleStartTag(tag.name!, tag.attrs!);
350
- else if (tag.kind === "startend") handlers.handleStartEndTag(tag.name!, tag.attrs!);
364
+ if (tag.kind === "start") {
365
+ const rawCloser = RAW_TEXT_CLOSERS[tag.name!];
366
+ if (rawCloser) {
367
+ const after = input.slice(tag.end);
368
+ const closeMatch = rawCloser.exec(after);
369
+ if (closeMatch) {
370
+ handlers.handleStartTag(tag.name!, tag.attrs!);
371
+ emitText(after.slice(0, closeMatch.index));
372
+ handlers.handleEndTag(tag.name!);
373
+ textStart = tag.end + closeMatch.index + closeMatch[0].length;
374
+ i = textStart;
375
+ continue;
376
+ }
377
+ }
378
+ handlers.handleStartTag(tag.name!, tag.attrs!);
379
+ } else if (tag.kind === "startend") handlers.handleStartEndTag(tag.name!, tag.attrs!);
351
380
  else if (tag.kind === "end") handlers.handleEndTag(tag.name!);
352
381
  textStart = tag.end;
353
382
  i = tag.end;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-unsloth-webtools",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "description": "Pi extension: web_search and web_fetch tools ported from the Unsloth Studio codebase (DuckDuckGo search, SSRF-safe direct fetching, HTML-to-Markdown extraction)",
6
6
  "main": "index.ts",