Skip to content

Commit

Permalink
feat: added spacing and space removing tags {s}, {s#}, {s0}. See docu…
Browse files Browse the repository at this point in the history
…mentation.
  • Loading branch information
EddieDover committed Feb 29, 2024
1 parent 0a5c646 commit 312f1b6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions TEMPLATE_README.md
Expand Up @@ -182,6 +182,9 @@ There are a few special keywords that must be surrounded by { } marks, to allow
* {newline} - Adds a line break to the text rendered.
* {charactersheet} - Inserts a clickable image of the character that will open their character sheet.
* {+} - Adds the values of two objects and outputs the result, i.e. `system.attributes.str {+} system.attributes.wis` will output the character's str and wis added together.
* {s} adds a space
* {s#} adds multiple spaces where # is the amount of spaces desired.
* {s0} will remove all spaces between it's preceeding and succeeding elements. E.g. `D{s0} system.attributes.str` becomes `D8`
* {i} & {/i} - Anything between these tags will be displayed in _italics_
* {b} & {/b} - Anything between these tags will be displayed in **bold**
* {u} & {/u} - Anything between these tags will be displayed as <u>underlined</u>
Expand Down
25 changes: 24 additions & 1 deletion src/module/utils.js
Expand Up @@ -2,7 +2,7 @@ import { DND5E } from "./systems/dnd5e";

var customSystems = [DND5E];
export var selectedSystem = null;
const NEWLINE_ELEMENTS = ["{newline}", "{nl}", ";"];
const NEWLINE_ELEMENTS = ["{newline}", "{nl}"];

/**
* Converts a string to proper case.
Expand Down Expand Up @@ -147,6 +147,29 @@ export function parseExtras(value, isSafeStringNeeded = false) {
value = value.replaceAll("{u}", "<u>").replaceAll("{/u}", "</u>");
}

// Detect if any text is surrounded with "{u} and {/u}" and replace with <b> tags
if (value.indexOf("{s}") > -1) {
isSafeStringNeeded = true;
value = value.replaceAll("{s}", "&nbsp;");
}

// Detect if the value contains {sX} where x is a digit and insert that many &nbsp; marks
let match = value.match(/\{s(\d+)\}/g);
if (match) {
for (const item of match) {
isSafeStringNeeded = true;
let amount = Number.parseInt(item.substring(2, item.length - 1));
if (amount > 0) {
value = value.replace(item, "&nbsp;".repeat(amount));
} else {
//If the amount is 0, then we want to trim all spaces before and after the {s0} tag
let before = value.substring(0, value.indexOf(item));
let after = value.substring(value.indexOf(item) + item.length);
value = before.trim() + after.trim();
}
}
}

//Parse out newline elements
for (const item of NEWLINE_ELEMENTS) {
if (value.indexOf(item) > -1) {
Expand Down

0 comments on commit 312f1b6

Please sign in to comment.