squiffy-compiler 6.0.0-beta.2 → 6.0.0-beta.4

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/dist/compiler.js +30 -8
  2. package/package.json +8 -7
package/dist/compiler.js CHANGED
@@ -298,6 +298,9 @@ export async function compile(settings) {
298
298
  additionalParameters
299
299
  };
300
300
  }
301
+ function escapeForHandlebarsString(s) {
302
+ return s.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
303
+ }
301
304
  async function processText(input, section, passage) {
302
305
  // Helper to get the next section name
303
306
  const getNextSectionName = () => {
@@ -313,7 +316,7 @@ export async function compile(settings) {
313
316
  settings.onWarning?.(`WARNING: ${section.filename} line ${section.line}: In section '${section.name}', there is a [[${text}>]] link but no following section exists`);
314
317
  return `[[${text}]]`; // fallback
315
318
  }
316
- return `{{section "${nextSectionName}" text="${text}"}}`;
319
+ return `{{section "${escapeForHandlebarsString(nextSectionName)}" text="${escapeForHandlebarsString(text)}"}}`;
317
320
  });
318
321
  // namedNextSectionLinkRegex matches [[text]](>) or [[text]](>, setter=value)
319
322
  // - a named link where target starts with >
@@ -326,7 +329,7 @@ export async function compile(settings) {
326
329
  }
327
330
  // rest could be empty or ", setter=value"
328
331
  const parsedName = getAdditionalLinkParameters(nextSectionName + rest);
329
- return `{{section "${parsedName.target}" text="${text}"${parsedName.additionalParameters}}}`;
332
+ return `{{section "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`;
330
333
  });
331
334
  // namedSectionLinkRegex matches:
332
335
  // open [[
@@ -340,21 +343,22 @@ export async function compile(settings) {
340
343
  checkSectionLinks(links, section, passage);
341
344
  input = input.replace(namedSectionLinkRegex, (_match, text /* $1 */, name /* $2 */) => {
342
345
  const parsedName = getAdditionalLinkParameters(name);
343
- return `{{section "${parsedName.target}" text="${text}"${parsedName.additionalParameters}}}`;
346
+ return `{{section "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`;
344
347
  });
345
348
  // namedPassageLinkRegex matches:
349
+ // not preceded by ! (to avoid matching image links like ![alt](src))
346
350
  // open [
347
351
  // any text - the link text
348
352
  // closing ]
349
353
  // open bracket, but not http(s):// after it
350
354
  // any text - the name of the passage
351
355
  // closing bracket
352
- const namedPassageLinkRegex = /\[([^\]]*?)\]\(((?!https?:\/\/).*?)\)/g;
356
+ const namedPassageLinkRegex = /(?<!!)\[([^\]]*?)\]\(((?!https?:\/\/).*?)\)/g;
353
357
  links = allMatchesForGroup(input, namedPassageLinkRegex, 2);
354
358
  checkPassageLinks(links, section, passage);
355
359
  input = input.replace(namedPassageLinkRegex, (_match, text /* $1 */, name /* $2 */) => {
356
360
  const parsedName = getAdditionalLinkParameters(name);
357
- return `{{passage "${parsedName.target}" text="${text}"${parsedName.additionalParameters}}}`;
361
+ return `{{passage "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`;
358
362
  });
359
363
  // unnamedSectionLinkRegex matches:
360
364
  // open [[
@@ -363,18 +367,36 @@ export async function compile(settings) {
363
367
  const unnamedSectionLinkRegex = /\[\[(.*?)\]\]/g;
364
368
  links = allMatchesForGroup(input, unnamedSectionLinkRegex, 1);
365
369
  checkSectionLinks(links, section, passage);
366
- input = input.replace(unnamedSectionLinkRegex, '{{section "$1"}}');
370
+ input = input.replace(unnamedSectionLinkRegex, (_match, name) => `{{section "${escapeForHandlebarsString(name)}"}}`);
367
371
  // unnamedPassageLinkRegex matches:
372
+ // not preceded by ! (to avoid matching image links like ![alt])
368
373
  // open [
369
374
  // any text - the link text
370
375
  // closing ]
371
376
  // no bracket after
372
- const unnamedPassageLinkRegex = /\[(.*?)\]([^(]|$)/g;
377
+ const unnamedPassageLinkRegex = /(?<!!)\[(.*?)\]([^(]|$)/g;
373
378
  links = allMatchesForGroup(input, unnamedPassageLinkRegex, 1);
374
379
  checkPassageLinks(links, section, passage);
375
- input = input.replace(unnamedPassageLinkRegex, '{{passage "$1"}}$2');
380
+ input = input.replace(unnamedPassageLinkRegex, (_match, name, suffix) => `{{passage "${escapeForHandlebarsString(name)}"}}${suffix}`);
381
+ checkEmbeds(input, section, passage);
376
382
  return input;
377
383
  }
384
+ function checkEmbeds(input, section, passage) {
385
+ if (!settings.onWarning)
386
+ return;
387
+ // embedRegex matches {{embed "name"}} or {{embed 'name'}} with a literal name.
388
+ // Like the runtime's embed helper, a name can refer to a passage in the
389
+ // current section or to any section.
390
+ const embedRegex = /\{\{~?\s*embed\s+(["'])(.*?)\1/g;
391
+ const embeds = allMatchesForGroup(input, embedRegex, 2);
392
+ const badEmbeds = embeds.filter(name => !(name in section.passages) && !(name in story.sections));
393
+ for (const badEmbed of badEmbeds) {
394
+ const location = passage
395
+ ? `${section.filename} line ${passage.line}: In section '${section.name}', passage '${passage.name}'`
396
+ : `${section.filename} line ${section.line}: In section '${section.name}'`;
397
+ settings.onWarning(`WARNING: ${location} there is an embed of {{embed "${badEmbed}"}}, but no section or passage with that name exists`);
398
+ }
399
+ }
378
400
  function allMatchesForGroup(input, regex, groupNumber) {
379
401
  const result = [];
380
402
  let match;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "squiffy-compiler",
3
- "version": "6.0.0-beta.2",
3
+ "version": "6.0.0-beta.4",
4
4
  "description": "A tool for creating multiple-choice interactive stories",
5
5
  "author": "Alex Warren",
6
6
  "contributors": [
@@ -10,15 +10,16 @@
10
10
  ],
11
11
  "repository": {
12
12
  "type": "git",
13
- "url": "git+https://github.com/textadventures/squiffy.git"
13
+ "url": "git+https://github.com/textadventures/squiffy.git",
14
+ "directory": "compiler"
14
15
  },
15
16
  "license": "MIT",
16
17
  "preferGlobal": true,
17
18
  "devDependencies": {
18
- "@types/node": "^24.3.0",
19
- "glob": "^11.0.3",
20
- "typescript": "^5.9.2",
21
- "vitest": "^3.2.4"
19
+ "@types/node": "^26.4.1",
20
+ "glob": "^13.0.6",
21
+ "typescript": "^6.0.3",
22
+ "vitest": "^5.0.3"
22
23
  },
23
24
  "scripts": {
24
25
  "test": "vitest --run",
@@ -31,5 +32,5 @@
31
32
  "dist"
32
33
  ],
33
34
  "type": "module",
34
- "gitHead": "d8f215aafa4534ddff8ecee0f8eaa4dd3eb9a46a"
35
+ "gitHead": "3c52f3730cf2a7f59f3a5ac76db42abd7024fb83"
35
36
  }