Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

SugarCube 2: how to alter passage text before it is rendered?

I've been messing around trying to get this to work for a while, but for the life of me I can't seem to figure out how to altar the raw text of a passage before it is rendered (to modify links, for instance).

For example, let's say I have this raw text in a passage:
This is my passage text; it is one sentence which contains a [[link|Passage2]] and some text.

Now let's say that before rendering I want to check if the passage that is about to be displayed has a link to Passage2, and change that link to be to Passage3. So before rendering I want to change the above text to:
This is my passage text; it is one sentence which contains a [[link|Passage3]] and some text.

So that the rendered link actually goes to Passage3.

I would think this should be handled via a simple javascript regex replace in a prerender task function somehow, but I can't seem to replace passage text using a prerender function... only append text to the passage.

Where/how can I do a simple search/replace of raw passage text before it is rendered?

Thanks!

Comments

  • edited March 2016
    You do not. SugarCube does not, at any point, make a copy of the original text of each passage, from the story data chunk within the DOM, it simply references the text as-is.

    Beyond that, I cannot fathom why you'd want to do what you're talking about in the first place. That sort of thing is, literally, why $variables and macros exist. For example:
    This is my passage text; it is one sentence which contains a [[link|$nextPassage]] and some text.
    
    Or:
    This is my passage text; it is one sentence which contains a \
    <<if someCondition>>[[link|Passage2]]\
    <<else anotherCondition>>[[link|Passage3]]\
    <<else>>[[link|Passage4]]\
    <</if>> \
    and some text.
    

    OK, yes. You could alter the encoded text of a passage within the story data chunk in DOM, but that's… crazy.
  • edited March 2016
    Thanks for your input, TME!
    Beyond that, I cannot fathom why you'd want to do what you're talking about in the first place. That sort of thing is, literally, why $variables and macros exist.

    Okay, yes, you got me! :) I guess I was thinking about more general regex text replacement/removal (than would typically be handled by variables), trying to figure out a solution to my typedjs problem, how to remove the caret pauses without breaking the links, thinking I could maybe do that before the links got turned into actual links... but it looks like I've been thinking about the whole "rendering" process all wrong. I'm sure that at least half of what I've been thinking is utterly crazy, and it's good to be told when one's a mad fool, instead of having to simply wonder. :)
    OK, yes. You could alter the encoded text of a passage within the story data chunk in DOM, but that's… crazy.

    Don't give me any ideas.
Sign In or Register to comment.