Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

[Cookbook][Utility Function] decodeEditorText() -- Strip out auto-inserted editor formatting and get back to bare text for bio, notes, gmnotes, etc

1535992863
The Aaron
Pro
API Scripter
The new in-game text editor is pretty nice, but comes with a problem for API Scripts that depend on reading information from one of the large text areas in the data set. This function helps with that. Given the text from a&nbsp; Graphic' s&nbsp; gmnotes &nbsp;property, or a&nbsp; Character' s&nbsp; bio &nbsp;or&nbsp; gmnotes &nbsp;property, or a&nbsp; Handout' s&nbsp; notes &nbsp;or&nbsp; gmnotes &nbsp;property, this will return a version with the auto-inserted editor formatting stripped out. const decodeEditorText = (t, o) = &gt; { let w = t; o = Object . assign ({ separator: '\r\n' , asArray: false },o); /* Token GM Notes */ if ( / ^%3Cp%3E / . test (w)){ w = unescape (w); } if ( / ^&lt;p&gt; / . test (w)){ let lines = w. match ( / &lt;p&gt;.*?&lt; \/ p&gt; / g ) . map ( l = &gt; l. replace ( / ^&lt;p&gt;(.*?)&lt; \/ p&gt;$ / ,'$ 1 ')); return o.asArray&nbsp;? lines&nbsp;: lines. join (o.separator); } /* neither */ return t; }; The first argument is the text to process. const text = decodeEditorText (token. get ( 'gmnotes' )); By default, the lines of text will be separated by&nbsp; \r\n . The optional second argument is an object with options. separator &nbsp;-- specifies what to separate lines of text with. Default:&nbsp; \r\n const text = decodeEditorText (token. get ( 'gmnotes' ),{separator: '&lt;BR&gt;' }); asArray &nbsp;-- specifies to instead return the lines as an array. Default:&nbsp; false const text = decodeEditorText (token. get ( 'gmnotes' ),{asArray: true }); NOTE: &nbsp; Nested&nbsp; &lt;p&gt; &nbsp;tags will confuse and break the decoding. If you run into that problem and need help, PM&nbsp; The Aaron &nbsp;and he'll be happy to look at it. In the wiki here: <a href="https://wiki.roll20.net/API:Cookbook#decodeEditorText" rel="nofollow">https://wiki.roll20.net/API:Cookbook#decodeEditorText</a>
1535993666

Edited 1535993754
The Aaron
Pro
API Scripter
Here's some examples.&nbsp; It automatically detects and deals with the escaped encoding used on Token GM Notes fields: { "type": "token:gmnotes", "t": "%3Cp%3Etest%20line%201%3C/p%3E%3Cp%3E%2523%20%2526%20%2523%20%2525%3C/p%3E%3Cp%3Etest%20line%203%3C/p%3E", "decodeEditorText(t)": "test line 1\r\n%23 %26 %23 %25\r\ntest line 3", "decodeEditorText(t,{separator: '&lt;BR/&gt;'})": "test line 1&lt;BR/&gt;%23 %26 %23 %25&lt;BR/&gt;test line 3", "decodeEditorText(t,{asArray: true})": [ "test line 1", "%23 %26 %23 %25", "test line 3" ] } Handouts are not escape encoded: { "type": "handout:gmnotes", "t": "&lt;p&gt;gmnotes first line&lt;/p&gt;&lt;p&gt;second line&lt;/p&gt;", "decodeEditorText(t)": "gmnotes first line\r\nsecond line", "decodeEditorText(t,{separator: '&lt;BR/&gt;'})": "gmnotes first line&lt;BR/&gt;second line", "decodeEditorText(t,{asArray: true})": [ "gmnotes first line", "second line" ] } { "type": "handout:notes", "t": "&lt;p&gt;notes first line &lt;/p&gt;&lt;p&gt;second line&lt;/p&gt;&lt;p&gt;special stuff: %23 %28&lt;/p&gt;", "decodeEditorText(t)": "notes first line \r\nsecond line\r\nspecial stuff: %23 %28", "decodeEditorText(t,{separator: '&lt;BR/&gt;'})": "notes first line &lt;BR/&gt;second line&lt;BR/&gt;special stuff: %23 %28", "decodeEditorText(t,{asArray: true})": [ "notes first line ", "second line", "special stuff: %23 %28" ] } Neither are Characters.&nbsp; Note preserving of the %23 %22 escape sequences: { "type": "character:gmnotes", "t": "&lt;p&gt;GM Notes first line&lt;/p&gt;&lt;p&gt;second line&lt;/p&gt;", "decodeEditorText(t)": "GM Notes first line\r\nsecond line", "decodeEditorText(t,{separator: '&lt;BR/&gt;'})": "GM Notes first line&lt;BR/&gt;second line", "decodeEditorText(t,{asArray: true})": [ "GM Notes first line", "second line" ] } { "type": "character:bio", "t": "&lt;p&gt;Some bio&lt;/p&gt;&lt;p&gt;%23 %22 %23&lt;/p&gt;&lt;p&gt;second line&lt;/p&gt;", "decodeEditorText(t)": "Some bio\r\n%23 %22 %23\r\nsecond line", "decodeEditorText(t,{separator: '&lt;BR/&gt;'})": "Some bio&lt;BR/&gt;%23 %22 %23&lt;BR/&gt;second line", "decodeEditorText(t,{asArray: true})": [ "Some bio", "%23 %22 %23", "second line" ] } Let me know if you have any questions!
Looks awesome!&nbsp; Unfortunately, I'm not sure how nice it'll play with my ongoing update to CashMaster so I may have to reinvent the wheel a bit, but if nothing else, this gives me a place to start!
1536000748
The Aaron
Pro
API Scripter
I don't see anywhere in the currently released CashMaster that you use bio/notes/gmnotes, but if you have a use case where you think it wouldn't work, definitely point it out so I can make sure I didn't miss something.
The current version is fine; it doesn't use those fields at all.&nbsp; The new update I'm actively working on, however, will.&nbsp; Any conflicts will likely be on my end though, so no need to worry!
1536001761

Edited 1536001946
The Aaron
Pro
API Scripter
Ah. Depending on what you're doing, you may find the array version handy: let CashTokenLines = decodeEditorText(token.get('gmnotes'),{asArray:true}); Easy-peasy line array. =D If you're using the gmnotes as a : separated key-value store: let CashOptions = decodeEditorText(token.get('gmnotes'),{asArray:true}).reduce((m,v)=&gt;{ let p=v.split(/:/); m[p[0]]=p.splice(1).join(':'); return m; },{});