Skip to content

Instantly share code, notes, and snippets.

@alfonsogarciacaro
Last active June 3, 2021 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfonsogarciacaro/ea8a3cfe07e773664ea5078141ca9f82 to your computer and use it in GitHub Desktop.
Save alfonsogarciacaro/ea8a3cfe07e773664ea5078141ca9f82 to your computer and use it in GitHub Desktop.
Fable JS tagged templates
open System
open System.Text.RegularExpressions
open Fable.Core
let getJsTemplate (s: FormattableString) =
let str = s.Format
let mutable prevIndex = 0
let matches = Regex.Matches(str, @"{\d+}")
Array.init (matches.Count + 1) (fun i ->
if i < matches.Count then
let m = matches.[i]
let idx = prevIndex
prevIndex <- m.Index + m.Length
str.Substring(idx, m.Index - idx)
else
str.Substring(prevIndex)), s.GetArguments()
type LitComponent = interface end
type Lit =
[<ImportMember("lit-html")>]
static member html(strs: string[], [<ParamArray>] args: obj[]): LitComponent = jsNative
let test() =
let name = "Angel"
let strs, args = getJsTemplate $"<div>Hello {name}</div>"
Lit.html(strs, args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment