@rohal12/twee-ts 1.10.0 → 1.11.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/dist/index.d.cts CHANGED
@@ -2,6 +2,7 @@
2
2
  * All public interfaces for twee-ts.
3
3
  */
4
4
  type OutputMode = 'html' | 'twee3' | 'twee1' | 'twine2-archive' | 'twine1-archive' | 'json';
5
+ type WordCountMethod = 'tweego' | 'whitespace';
5
6
  type InlineSource = {
6
7
  filename: string;
7
8
  content: string | Buffer;
@@ -40,6 +41,8 @@ interface CompileOptions {
40
41
  tagAliases?: Record<string, string>;
41
42
  /** Emit source file and line as data- attributes on passage elements. Default: false. */
42
43
  sourceInfo?: boolean;
44
+ /** Word counting method. Default: 'tweego'. */
45
+ wordCountMethod?: WordCountMethod;
43
46
  }
44
47
  interface CompileToFileOptions extends CompileOptions {
45
48
  /** Output file path. */
@@ -184,6 +187,7 @@ interface TweeTsConfig {
184
187
  noRemote?: boolean;
185
188
  tagAliases?: Record<string, string>;
186
189
  sourceInfo?: boolean;
190
+ wordCountMethod?: WordCountMethod;
187
191
  }
188
192
 
189
193
  declare class TweeTsError extends Error {
@@ -438,4 +442,4 @@ declare function getCacheSize(): {
438
442
  count: number;
439
443
  };
440
444
 
441
- export { type BrokenLink, CONFIG_FILENAME, type CachedFormatEntry, type CompileOptions, type CompileResult, type CompileStats, type CompileToFileOptions, type Diagnostic, type FileCacheEntry, type InlineSource, ItemType, type LexerItem, type LintResult, type OutputMode, type Passage, type PassageMetadata, type SFAIndex, type SFAIndexEntry, type SourceInput, type SourceLocation, type Story, type StoryFormatInfo, type StoryMap, TweeLexer, type TweeTsConfig, TweeTsError, type WatchOptions, applyTagAliases, clearCachedFormats, compile, compileIncremental, compileToFile, discoverCachedFormats, discoverFormats, fetchAndCacheFormat, fetchDirectFormat, formatLintReport, generateIFID, getCacheDir, getCacheSize, getFormatSearchDirs, lint, listCachedFormats, loadConfig, loadConfigFile, parseFormatJSON, parseSemver, parseTwee, resolveRemoteFormat, scaffoldConfig, semverCompare, storyInspect, tweeLexer, validateConfig, validateIFID, watch };
445
+ export { type BrokenLink, CONFIG_FILENAME, type CachedFormatEntry, type CompileOptions, type CompileResult, type CompileStats, type CompileToFileOptions, type Diagnostic, type FileCacheEntry, type InlineSource, ItemType, type LexerItem, type LintResult, type OutputMode, type Passage, type PassageMetadata, type SFAIndex, type SFAIndexEntry, type SourceInput, type SourceLocation, type Story, type StoryFormatInfo, type StoryMap, TweeLexer, type TweeTsConfig, TweeTsError, type WatchOptions, type WordCountMethod, applyTagAliases, clearCachedFormats, compile, compileIncremental, compileToFile, discoverCachedFormats, discoverFormats, fetchAndCacheFormat, fetchDirectFormat, formatLintReport, generateIFID, getCacheDir, getCacheSize, getFormatSearchDirs, lint, listCachedFormats, loadConfig, loadConfigFile, parseFormatJSON, parseSemver, parseTwee, resolveRemoteFormat, scaffoldConfig, semverCompare, storyInspect, tweeLexer, validateConfig, validateIFID, watch };
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  * All public interfaces for twee-ts.
3
3
  */
4
4
  type OutputMode = 'html' | 'twee3' | 'twee1' | 'twine2-archive' | 'twine1-archive' | 'json';
5
+ type WordCountMethod = 'tweego' | 'whitespace';
5
6
  type InlineSource = {
6
7
  filename: string;
7
8
  content: string | Buffer;
@@ -40,6 +41,8 @@ interface CompileOptions {
40
41
  tagAliases?: Record<string, string>;
41
42
  /** Emit source file and line as data- attributes on passage elements. Default: false. */
42
43
  sourceInfo?: boolean;
44
+ /** Word counting method. Default: 'tweego'. */
45
+ wordCountMethod?: WordCountMethod;
43
46
  }
44
47
  interface CompileToFileOptions extends CompileOptions {
45
48
  /** Output file path. */
@@ -184,6 +187,7 @@ interface TweeTsConfig {
184
187
  noRemote?: boolean;
185
188
  tagAliases?: Record<string, string>;
186
189
  sourceInfo?: boolean;
190
+ wordCountMethod?: WordCountMethod;
187
191
  }
188
192
 
189
193
  declare class TweeTsError extends Error {
@@ -438,4 +442,4 @@ declare function getCacheSize(): {
438
442
  count: number;
439
443
  };
440
444
 
441
- export { type BrokenLink, CONFIG_FILENAME, type CachedFormatEntry, type CompileOptions, type CompileResult, type CompileStats, type CompileToFileOptions, type Diagnostic, type FileCacheEntry, type InlineSource, ItemType, type LexerItem, type LintResult, type OutputMode, type Passage, type PassageMetadata, type SFAIndex, type SFAIndexEntry, type SourceInput, type SourceLocation, type Story, type StoryFormatInfo, type StoryMap, TweeLexer, type TweeTsConfig, TweeTsError, type WatchOptions, applyTagAliases, clearCachedFormats, compile, compileIncremental, compileToFile, discoverCachedFormats, discoverFormats, fetchAndCacheFormat, fetchDirectFormat, formatLintReport, generateIFID, getCacheDir, getCacheSize, getFormatSearchDirs, lint, listCachedFormats, loadConfig, loadConfigFile, parseFormatJSON, parseSemver, parseTwee, resolveRemoteFormat, scaffoldConfig, semverCompare, storyInspect, tweeLexer, validateConfig, validateIFID, watch };
445
+ export { type BrokenLink, CONFIG_FILENAME, type CachedFormatEntry, type CompileOptions, type CompileResult, type CompileStats, type CompileToFileOptions, type Diagnostic, type FileCacheEntry, type InlineSource, ItemType, type LexerItem, type LintResult, type OutputMode, type Passage, type PassageMetadata, type SFAIndex, type SFAIndexEntry, type SourceInput, type SourceLocation, type Story, type StoryFormatInfo, type StoryMap, TweeLexer, type TweeTsConfig, TweeTsError, type WatchOptions, type WordCountMethod, applyTagAliases, clearCachedFormats, compile, compileIncremental, compileToFile, discoverCachedFormats, discoverFormats, fetchAndCacheFormat, fetchDirectFormat, formatLintReport, generateIFID, getCacheDir, getCacheSize, getFormatSearchDirs, lint, listCachedFormats, loadConfig, loadConfigFile, parseFormatJSON, parseSemver, parseTwee, resolveRemoteFormat, scaffoldConfig, semverCompare, storyInspect, tweeLexer, validateConfig, validateIFID, watch };
package/dist/index.js CHANGED
@@ -229,15 +229,32 @@ function passageToTiddler(p, pid, obfuscateRot13 = false) {
229
229
  const nameComment = obfuscateRot13 ? `<!-- ${commentSanitize(p.name)} -->` : "";
230
230
  return `<div tiddler=${quote(attrEscape(p.name))} tags=${quote(attrEscape(p.tags.join(" ")))} created=${quote(created)} modifier=${quote("twee")} twine-position=${quote(attrEscape(position))}>${nameComment}${content}</div>`;
231
231
  }
232
- function countWords(p) {
233
- let text = p.text;
234
- text = text.replace(/\n/g, "");
235
- text = text.replace(/(?:\/%.+?%\/|\/\*.+?\*\/|<!--.+?-->)/gs, "");
236
- const normalized = text.normalize("NFKD");
237
- const count = [...normalized].length;
238
- if (count === 0) return 0;
239
- const words = Math.floor(count / 5);
240
- return count % 5 > 0 ? words + 1 : words;
232
+ function countWords(p, method = "tweego") {
233
+ switch (method) {
234
+ case "tweego": {
235
+ let text = p.text;
236
+ text = text.replace(/\n/g, "");
237
+ text = text.replace(/(?:\/%.+?%\/|\/\*.+?\*\/|<!--.+?-->)/gs, "");
238
+ const normalized = text.normalize("NFKD");
239
+ const count = [...normalized].length;
240
+ if (count === 0) return 0;
241
+ const words = Math.floor(count / 5);
242
+ return count % 5 > 0 ? words + 1 : words;
243
+ }
244
+ case "whitespace": {
245
+ let text = p.text;
246
+ text = text.replace(/(?:\/%.+?%\/|\/\*.+?\*\/|<!--.+?-->)/gs, "");
247
+ text = text.replace(/<<[^>]*>>/g, "");
248
+ text = text.replace(/\[\[([^\]|]*?)(?:\|[^\]]*?)?\]\]/g, "$1");
249
+ text = text.replace(/<[^>]+>/g, "");
250
+ const tokens = text.split(/\s+/).filter((t) => t.length > 0);
251
+ return tokens.length;
252
+ }
253
+ default: {
254
+ const _exhaustive = method;
255
+ throw new Error(`Unhandled word count method: ${_exhaustive}`);
256
+ }
257
+ }
241
258
  }
242
259
  function applyTagAliases(passages, aliases) {
243
260
  const entries = Object.entries(aliases);
@@ -455,13 +472,13 @@ function storyAdd(story, p, diagnostics) {
455
472
  }
456
473
  storyAppend(story, processed, diagnostics);
457
474
  }
458
- function getStoryStats(story) {
475
+ function getStoryStats(story, wordCountMethod = "tweego") {
459
476
  let storyPassages = 0;
460
477
  let words = 0;
461
478
  for (const p of story.passages) {
462
479
  if (isStoryPassage(p)) {
463
480
  storyPassages++;
464
- words += countWords(p);
481
+ words += countWords(p, wordCountMethod);
465
482
  }
466
483
  }
467
484
  return { passages: story.passages.length, storyPassages, words };
@@ -2305,7 +2322,7 @@ async function buildOutput(options, cache, changedFiles) {
2305
2322
  }
2306
2323
  }
2307
2324
  const stats = {
2308
- ...getStoryStats(story),
2325
+ ...getStoryStats(story, options.wordCountMethod),
2309
2326
  files: [...processedFiles]
2310
2327
  };
2311
2328
  return { output, story, format, diagnostics, stats };
@@ -2526,6 +2543,7 @@ import { readFileSync as readFileSync4, existsSync as existsSync2 } from "fs";
2526
2543
  import { join as join5 } from "path";
2527
2544
  var CONFIG_FILENAME = "twee-ts.config.json";
2528
2545
  var VALID_OUTPUT_MODES = ["html", "twee3", "twee1", "twine2-archive", "twine1-archive", "json"];
2546
+ var VALID_WORD_COUNT_METHODS = ["tweego", "whitespace"];
2529
2547
  function loadConfig(dir) {
2530
2548
  const base = dir ?? process.cwd();
2531
2549
  const configPath = join5(base, CONFIG_FILENAME);
@@ -2612,6 +2630,11 @@ function validateConfig(data) {
2612
2630
  errors.push(`"outputMode" must be one of: ${VALID_OUTPUT_MODES.join(", ")}.`);
2613
2631
  }
2614
2632
  }
2633
+ if ("wordCountMethod" in obj) {
2634
+ if (typeof obj["wordCountMethod"] !== "string" || !VALID_WORD_COUNT_METHODS.includes(obj["wordCountMethod"])) {
2635
+ errors.push(`"wordCountMethod" must be one of: ${VALID_WORD_COUNT_METHODS.join(", ")}.`);
2636
+ }
2637
+ }
2615
2638
  return errors;
2616
2639
  }
2617
2640
  function scaffoldConfig() {