squiffy-compiler 6.0.0-beta.1 → 6.0.0-beta.3

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 +43 -9
  2. package/package.json +8 -7
package/dist/compiler.js CHANGED
@@ -93,6 +93,7 @@ export async function compile(settings) {
93
93
  }
94
94
  const regexes = {
95
95
  section: /^\[\[(.*)\]\]:$/,
96
+ snippet: /^\[snippet:\s*(.+)\]:$/,
96
97
  passage: /^\[(.*)\]:$/,
97
98
  title: /^@title (.*)$/,
98
99
  import: /^@import (.*)$/,
@@ -112,6 +113,7 @@ export async function compile(settings) {
112
113
  let passage = null; // annotated differently to section, as a workaround for TypeScript "Property does not exist on type never"
113
114
  let textStarted = false;
114
115
  let inUiBlock = false;
116
+ let inSnippet = false;
115
117
  const ensureThisSectionExists = () => {
116
118
  return ensureSectionExists(section, isFirst, inputFilename, lineCount);
117
119
  };
@@ -134,11 +136,21 @@ export async function compile(settings) {
134
136
  match[key] = result;
135
137
  }
136
138
  }
137
- if (match.section) {
139
+ if (match.snippet) {
140
+ inSnippet = true;
141
+ section = null;
142
+ passage = null;
143
+ textStarted = false;
144
+ }
145
+ else if (match.section) {
146
+ inSnippet = false;
138
147
  section = story.addSection(match.section[1], inputFilename, lineCount);
139
148
  passage = null;
140
149
  textStarted = false;
141
150
  }
151
+ else if (inSnippet) {
152
+ // skip all content inside snippet blocks
153
+ }
142
154
  else if (match.passage) {
143
155
  if (!section) {
144
156
  errors.push(`ERROR: ${inputFilename} line ${lineCount}: Can't add passage "${match.passage[1]}" as no section has been created.`);
@@ -286,6 +298,9 @@ export async function compile(settings) {
286
298
  additionalParameters
287
299
  };
288
300
  }
301
+ function escapeForHandlebarsString(s) {
302
+ return s.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
303
+ }
289
304
  async function processText(input, section, passage) {
290
305
  // Helper to get the next section name
291
306
  const getNextSectionName = () => {
@@ -301,7 +316,7 @@ export async function compile(settings) {
301
316
  settings.onWarning?.(`WARNING: ${section.filename} line ${section.line}: In section '${section.name}', there is a [[${text}>]] link but no following section exists`);
302
317
  return `[[${text}]]`; // fallback
303
318
  }
304
- return `{{section "${nextSectionName}" text="${text}"}}`;
319
+ return `{{section "${escapeForHandlebarsString(nextSectionName)}" text="${escapeForHandlebarsString(text)}"}}`;
305
320
  });
306
321
  // namedNextSectionLinkRegex matches [[text]](>) or [[text]](>, setter=value)
307
322
  // - a named link where target starts with >
@@ -314,7 +329,7 @@ export async function compile(settings) {
314
329
  }
315
330
  // rest could be empty or ", setter=value"
316
331
  const parsedName = getAdditionalLinkParameters(nextSectionName + rest);
317
- return `{{section "${parsedName.target}" text="${text}"${parsedName.additionalParameters}}}`;
332
+ return `{{section "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`;
318
333
  });
319
334
  // namedSectionLinkRegex matches:
320
335
  // open [[
@@ -328,21 +343,22 @@ export async function compile(settings) {
328
343
  checkSectionLinks(links, section, passage);
329
344
  input = input.replace(namedSectionLinkRegex, (_match, text /* $1 */, name /* $2 */) => {
330
345
  const parsedName = getAdditionalLinkParameters(name);
331
- return `{{section "${parsedName.target}" text="${text}"${parsedName.additionalParameters}}}`;
346
+ return `{{section "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`;
332
347
  });
333
348
  // namedPassageLinkRegex matches:
349
+ // not preceded by ! (to avoid matching image links like ![alt](src))
334
350
  // open [
335
351
  // any text - the link text
336
352
  // closing ]
337
353
  // open bracket, but not http(s):// after it
338
354
  // any text - the name of the passage
339
355
  // closing bracket
340
- const namedPassageLinkRegex = /\[([^\]]*?)\]\(((?!https?:\/\/).*?)\)/g;
356
+ const namedPassageLinkRegex = /(?<!!)\[([^\]]*?)\]\(((?!https?:\/\/).*?)\)/g;
341
357
  links = allMatchesForGroup(input, namedPassageLinkRegex, 2);
342
358
  checkPassageLinks(links, section, passage);
343
359
  input = input.replace(namedPassageLinkRegex, (_match, text /* $1 */, name /* $2 */) => {
344
360
  const parsedName = getAdditionalLinkParameters(name);
345
- return `{{passage "${parsedName.target}" text="${text}"${parsedName.additionalParameters}}}`;
361
+ return `{{passage "${escapeForHandlebarsString(parsedName.target)}" text="${escapeForHandlebarsString(text)}"${parsedName.additionalParameters}}}`;
346
362
  });
347
363
  // unnamedSectionLinkRegex matches:
348
364
  // open [[
@@ -351,18 +367,36 @@ export async function compile(settings) {
351
367
  const unnamedSectionLinkRegex = /\[\[(.*?)\]\]/g;
352
368
  links = allMatchesForGroup(input, unnamedSectionLinkRegex, 1);
353
369
  checkSectionLinks(links, section, passage);
354
- input = input.replace(unnamedSectionLinkRegex, '{{section "$1"}}');
370
+ input = input.replace(unnamedSectionLinkRegex, (_match, name) => `{{section "${escapeForHandlebarsString(name)}"}}`);
355
371
  // unnamedPassageLinkRegex matches:
372
+ // not preceded by ! (to avoid matching image links like ![alt])
356
373
  // open [
357
374
  // any text - the link text
358
375
  // closing ]
359
376
  // no bracket after
360
- const unnamedPassageLinkRegex = /\[(.*?)\]([^(]|$)/g;
377
+ const unnamedPassageLinkRegex = /(?<!!)\[(.*?)\]([^(]|$)/g;
361
378
  links = allMatchesForGroup(input, unnamedPassageLinkRegex, 1);
362
379
  checkPassageLinks(links, section, passage);
363
- input = input.replace(unnamedPassageLinkRegex, '{{passage "$1"}}$2');
380
+ input = input.replace(unnamedPassageLinkRegex, (_match, name, suffix) => `{{passage "${escapeForHandlebarsString(name)}"}}${suffix}`);
381
+ checkEmbeds(input, section, passage);
364
382
  return input;
365
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
+ }
366
400
  function allMatchesForGroup(input, regex, groupNumber) {
367
401
  const result = [];
368
402
  let match;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "squiffy-compiler",
3
- "version": "6.0.0-beta.1",
3
+ "version": "6.0.0-beta.3",
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": "^4.1.11"
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": "da6023a5cde6b179ce756f8f03bfc2b4ff7a0274"
35
+ "gitHead": "d932cf0684a7fa941dc8097bcc430761949e07fb"
35
36
  }