Last active
June 3, 2021 03:59
-
-
Save alfonsogarciacaro/ea8a3cfe07e773664ea5078141ca9f82 to your computer and use it in GitHub Desktop.
Fable JS tagged templates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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