1import { Controller } from "@hotwired/stimulus" 2 3export default class extends Controller { 4 static values = { 5 url: { type: String, default: "/action_text/embeds" } // endpoint that handles unfurling 6 } 7 8 unfurl(event) { 9 this.#unfurlLink(event.detail.url, event.detail) 10 } 11 12 async #unfurlLink(url, callbacks) { 13 const response = await fetch(this.urlValue, { 14 method: "POST", 15 body: JSON.stringify({ id: event.detail.url }), 16 headers: { 17 "Content-Type": "application/json", 18 "Accept": "application/json", 19 "X-CSRF-Token": document.querySelector("[name='csrf-token']").content 20 } 21 }) 22 23 const metadata = await response.json() 24 this.#insertUnfurledLink(metadata, callbacks) 25 } 26 27 #insertUnfurledLink(metadata, callbacks) { 28 // Replace the pasted link with your custom HTML 29 // callbacks.replaceLinkWith(this.#renderUnfurledLinkHTML(metadata)) 30 31 // Or, insert below the link as an attachment: 32 callbacks.insertBelowLink(metadata.content, { attachment: { sgid: metadata.sgid } }) 33 } 34 35 #renderUnfurledLinkHTML(link) { 36 return `<a href="${link.canonical_url}">${link.title}</a>` 37 } 38}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.