silverbulletmd / silverbullet

The hackable notebook
https://silverbullet.md
MIT License
2k stars 138 forks source link

Intl.Segmenter isn't available in Firefox #818

Closed PatrikStenmark closed 2 days ago

PatrikStenmark commented 3 months ago

It seems that the changes in 89e2e7a broke Silverbullet running in Firefox, due to the usage of Intl.Segmenter. This is only available in Firefox Nightly.

Is there any thoughts around browser compatibility?

Would it be a good idea to add a check for Intl.Segmenter availability and fall back to just splitting by whitespace and taking the n words before/after the index? I might be able to make a PR for that if that seems like a good idea.

zefhemel commented 3 months ago

Actually we polyfill Intl and Temporal already in the client (the browser's main thread), so it's already available to Space Script: https://github.com/silverbulletmd/silverbullet/blob/main/common/space_script.ts#L12 , what I missed is that this polyfill isn't available in web workers, which plugs run in 🤦🏻

Two possible fixes: also expose this polyfill in plugs code, the other is indeed to fall back to a simpler implementation. I'd have to check how big the polyfill is (because this size would be added to each individual plug JS bundle), but perhaps the fallback you suggest is the more pragmatic solution.

zefhemel commented 3 months ago

Thinking about this more: let's go with the fallback approach, if you could give that a try I'd much appreciate it!

PatrikStenmark commented 3 months ago

I'm willing to try. It might take a couple of days though. My computer decided to spontaneously die so I have to wait for a new one.

daniel-michel commented 2 months ago

I just tried to create a function for displaying a relative time and noticed the issue that I cannot use Intl.RelativeTimeFormat.

It looks like the Intl exported from js-temporal/polyfill exports an Intl object which only has the DateTimeFormat. Also it maybe does not polyfill it, but just exports the original one? https://github.com/js-temporal/temporal-polyfill/blob/e1a1452b712dbaddf1fb856b869667510b6768fc/lib/intl.ts#L41

I guess this also causes some of the properties from Intl to be removed in a space-script. I get this in the terminal when logging Intl in a space-script:

[Object: null prototype] {
  DateTimeFormat: [Function: DateTimeFormat] {
    supportedLocalesOf: [Function (anonymous)]
  }
}
This is the script I tried to write to get relative times. ```js silverbullet.registerFunction("relativeTime", (time, baseTime = new Date()) => { const relForm = new Intl.RelativeTimeFormat('en'); const relative = (+new Date(time) - +new Date(baseTime)) / 1000; const absDiff = Math.abs(relative); const units = [ [1, "second"], [60, "minute"], [60 * 60, "hour"], [60 * 60 * 24, "day"], [60 * 60 * 24 * 7, "week"], [60 * 60 * 24 * 30, "month"], [60 * 60 * 24 * 365, "year"], ]; if (absDiff < 1) { return relative < 0 ? "just now" : "now"; } for (let i = 0; i < units.length; i++) { const [factor, unit] = units[i]; if (i === units.length - 1 || absDiff / units[i + 1][0] < 1) { return relForm.format(relative / factor, unit); } } }); ```
zefhemel commented 2 months ago

@daniel-michel I think you fixed this since with #836 correct?

daniel-michel commented 2 months ago

Yes