Skip to content

Commit

Permalink
Replacing internal escaping with external-based escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Cox authored and Dan Cox committed Jan 16, 2024
1 parent 93ed8f1 commit 93b224b
Showing 1 changed file with 5 additions and 26 deletions.
31 changes: 5 additions & 26 deletions src/Twine2HTML/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function parse (content) {
// Did we find any elements?
if (storyDataElements.length === 0) {
// If there is not a single `<tw-storydata>` element, this is not a Twine 2 story!
throw new Error('Not Twine 2 HTML content!');
throw new Error('TypeError: Not Twine 2 HTML content!');
}

// We only parse the first element found.
Expand Down Expand Up @@ -189,9 +189,9 @@ function parse (content) {
// Does name exist?
if (Object.prototype.hasOwnProperty.call(attr, 'name')) {
// Escape the name
name = escapeMetacharacters(attr.name);
name = attr.name;
} else {
throw new Error('Cannot parse passage data without name!');
throw new Error('TypeError: Cannot parse passage data without name!');
}

// Create empty tag array.
Expand All @@ -202,7 +202,7 @@ function parse (content) {
// (Attributes can, themselves, be empty strings.)
if (attr.tags.length > 0 && attr.tags !== '""') {
// Escape the tags
tags = escapeMetacharacters(attr.tags);
tags = attr.tags;
// Split by spaces into an array
tags = tags.split(' ');
}
Expand Down Expand Up @@ -328,25 +328,4 @@ function parse (content) {
return story;
}

/**
* Try to escape Twine 2 meta-characters.
* @param {string} result - Text to parse.
* @returns {string} Escaped characters.
*/
function escapeMetacharacters (result) {
// Replace any single backslash, \, with two of them, \\.
result = result.replace(/\\/g, '\\');
// Double-escape escaped {
result = result.replace(/\\\{/g, '\\\\{');
// Double-escape escaped }
result = result.replace(/\\\}/g, '\\\\}');
// Double-escape escaped [
result = result.replace(/\\\[/g, '\\\\[');
// Double-escape escaped ]
result = result.replace(/\\\]/g, '\\\\]');

return result;
}

export { parse, escapeMetacharacters };
export default parse;
export { parse };

0 comments on commit 93b224b

Please sign in to comment.