0 votes
by (280 points)
edited by

I want to append some texts after specific letter,

so I made some script below

Macro.add('lr', {
	tags : null,
	handler: function() {
		var content = this.payload[0].contents
		var len = content.length - 1
		var str = (content.charCodeAt(len));
		if(str==108)
			{str = content + "an"}
		else if(str==114){str = content + "a"}
		$(this.output).wiki(str);
	}
});

 

in passage,

 

<<lr>>steal<</lr>>
<<lr>>ser<</lr>>

<<set $a to "steal">>
<<set $b to "ser">>

<<lr>>$a<</lr>>
<<lr>>$b<</lr>>

 

first case, which I wrote some text directly, it works perfectly

but with variables, it work with last letter of variabale name not contents of that.

1 Answer

+1 vote
by (44.7k points)
selected by
 
Best answer

If you want the content to be parsed first, then you'll need to add a little bit of trickery to do it:

Macro.add('lr', {
	tags: null,
	handler: function() {
		var content = this.payload[0].contents;
		content = $(document.createElement("div")).wiki(content).get(0).innerHTML;
		var str = content.charCodeAt(content.length - 1);
		if (str == 108) {
			$(this.output).wiki(content + "an");
		} else if (str == 114) {
			$(this.output).wiki(content + "a");
		}
	}
});

That will "wikify" the contents first, before passing that parsed text as a string.

Hope that helps!  :-)

by (280 points)
edited by

thanks for answer but unfortunately, it doesn't work and return charcode 62 which mean ">" both of "steal" and "ser"

I found that 'var content' contains below instead of the result value

<span title="$a" aria-label="$a" data-type="variable" data-name="$a" class="debug">

steal</span>

<wbr class="debug hidden">

 

by (44.7k points)
edited by

That only happens if you're running it in "test mode".  If you run it normally then it works just fine.

If you modify the above code like this:

		} else if (str == 114) {
			$(this.output).wiki(content + "a");
		} else {
			$(this.output).wiki("{{{" + content + "}}}");
		}

and run it both normally and in test mode you can see that the HTML is just extra debugger content that only appears in test mode.

by (280 points)
what a debugger!

it works well in play mode! thank you so much
...