Custom Formatters (Under Node.js and Single-File HTML)

A big phone project at work has motivated me to finally do something I’ve wanted to do for a long time: create a customer phonenumber format I can apply to unformatted number strings. The idea is to take phone numbers stored in tiddler fields in the format 1234567890 and display them as (123) 456-7890.

To do this, I searched up the TWcore’s dateformat module, poked and prodded, consulted the usual sites, trialed-and-errored, and eventually quizzed Microsoft Copilot on why my plugin-to-be isn’t working.

Copilot seems to think the problem is that my formatters aren’t being registered by TiddlyWiki under Node.js — how I normally use TW — and that, to get the formatter modules to register, I should install a copy of the tiddlywiki/core plugin in my wiki folder.

I could be wrong, but that doesn’t seem like the root of my problem. I don’t think I should have to update a local copy of the core plugin every time I update TiddlyWiki globally under Node.

Normally, updating under Node updates all my core plugins. I call those native/core plugins explicitly from the global store in the tiddlywiki.info file for each new wiki I create. But the core itself doesn’t exist in the npm\node_modules\tiddlywiki\plugins folder, so I don’t explicitly include it in my plugin calls in tiddlywiki.info files. Maybe I could/should do that — but I’m not convinced, at least not by an AI assistant.

Plus, I want my custom formatters to work in single-file HTML wikis, too — and when I export what I currently have to a single-file wiki, it still doesn’t work. Which means that if the problem is the formatters not being registered, they aren’t going to get registered when I load a single-file wiki, either — regardless of my Node setup back on my desktop.

Does anyone have any insights?

Or any good custom formatters of their own I could cheat off of? :wink:

You can see what I’ve cobbled together so far here, if you’d like to take a look: (All the constituent tiddlers can be accessed in the Tiddlers list in the sidebar.)
https://tiddlywiki.secret-hq.com/plugins/telephone-numbers

1 Like

Never put things under “physical folders”, and never need to edit tiddlywiki.info file. You only need to manage everything just inside your wiki.
This old-fashion way pollute the AI learning materials, while it is already deprecated, and is very unfriendly to normal users. See [IDEA] Affordance for installing plugins on node.js · Issue #9210 · TiddlyWiki/TiddlyWiki5 · GitHub

For a custom formatters, you can let your code copilot write a “JS macro” that have 1234567890 input and have (123) 456-7890 output. You may need to provide an example for it How can I create a Javascript macro?
Use macrocall widget, and transclude the number field as your js macro’s input.
You can add these to a view template tiddler, so it appears on every tiddler.

And package it with Why Gatha is a Game Changer? - #25 by Mohammad , as a json plugin, then you can use json plugin in nodejs wiki and html wiki.

2 Likes

I probably don’t understand the question because to me it sounds like you should just use some string operations to snip out a few segments from the number at the time to e.g wrap the first 3 digits it in parentheses, etc? Is that not what you want?

Here’s a solution that doesn’t use any custom formatters…

\function format.phone()
[split[]lowercase[]]
+[search-replace::regexp[a|b|c],[2]]
+[search-replace::regexp[d|e|f],[3]]
+[search-replace::regexp[g|h|i],[4]]
+[search-replace::regexp[j|k|l],[5]]
+[search-replace::regexp[m|n|o],[6]]
+[search-replace::regexp[p|q|r|s],[7]]
+[search-replace::regexp[t|u|v],[8]]
+[search-replace::regexp[w|x|y|z],[9]]
+[join[]]
+[search-replace::regexp[^(\d{3})(\d{3})(\d{4})],[($1) $2-$3]]
+[search-replace::regexp[^(\d{3})(\d{4})],[$1-$2]]
\end

TESTS:
{{{ [[1234567890]format.phone[]] }}}

{{{ [[4567890]format.phone[]] }}}

{{{ [[savings]format.phone[]] }}}

Notes:

  • The format.phone() function starts by splitting the input into separate characters and converting any letters to lower case
  • Then use a series of string-replace::regexp[...],[#] to convert letters to their corresponding phone dial numbers
  • Then join the now number-only character sequences back into whole phone number strings
  • Then, +[search-replace::regexp[^(\d{3})(\d{3})(\d{4})],[($1) $2-$3]] converts 10-digit number strings into formatted phone numbers with area code (e.g., “(123) 456-7890”). Note that this does NOT match 7-digit numbers, which are left unchanged by this step.
  • Then, +[search-replace::regexp[^(\d{3})(\d{4})],[$1-$2]] converts any 7-digit number string into formatted phone numbers without area code (e.g., “456-7890”).

enjoy,
-e

1 Like

@EricShulman - interesting. What’s the point with the conversion to letters?Is it perhaps that sometimes phone numbers are expressed as words (esp. in the US)?

The conversion to letters is because @Secret-HQ’s spec (see the link he posted) says he wants to convert “vanity” letters to numbers. Also, letters are sometimes used in phone numbers in commercial advertising…

There was an old advert for “Mattress Discounters” which said:

Plus, back when I had a landline phone number… it ended in “3742”, which spelled “ERIC”

-e

2 Likes