Service for adding interactive features to content rendered from the Lynkow API.

Accessible via lynkow.enhancements. This is a browser-only service -- all methods are no-ops on the server. Currently provides:

  • Copy button for code blocks (elements with [data-copy-code] attribute)

  • Script activation for inline scripts injected via dangerouslySetInnerHTML

  • Widget iframe auto-resize for embedded Lynkow widgets

  • CSS normalization to ensure content renders correctly with CSS frameworks (Tailwind, etc.)

A MutationObserver automatically detects new content added to the DOM and applies enhancements, making it compatible with SPA frameworks like React/Next.js. Script clones are appended to <head> (not inline) to avoid React DOM reconciliation issues.

Access via: lynkow.enhancements

Methods

3 methods

destroy

TypeScript
destroy(): void

Clean up every resource the enhancements service owns: disconnect the MutationObserver, remove the widget resize listener, cancel any pending animation frame, remove injected styles and cloned scripts from <head>, and reset the initialized state. Safe to call repeatedly; subsequent calls are no-ops. After destroy() you can re-attach by calling init() again. No-op on server.

Returns: void

TypeScript
useEffect(() => {
  lynkow.enhancements.init()
  return () => lynkow.enhancements.destroy()
}, [])

init

TypeScript
init(): void

Initializes content enhancements: injects CSS styles, binds copy-to-clipboard handlers on code blocks, activates inline scripts, starts the widget iframe resize listener, and sets up a MutationObserver to automatically enhance newly added DOM content. Idempotent -- calling multiple times has no effect after the first initialization. No-op on server.

Call this manually if you need to re-initialize after the client is created (e.g. after dynamically loading content outside the initial render).

Returns: void

TypeScript
// Reinitialize enhancements after dynamically loading new content
const html = await fetchArticleContent()
document.getElementById('article').innerHTML = html
lynkow.enhancements.init()

isInitialized

TypeScript
isInitialized(): boolean

Check whether the enhancements service has been initialized for the current page. Returns false on the server and after a call to destroy (until init() is invoked again).

Returns: boolean

TypeScript
if (!lynkow.enhancements.isInitialized()) {
  lynkow.enhancements.init()
}