Internally we've built a set of utility helpers to streamline common tasks across the codebase. These utilities are designed to be small, focused functions that can be reused in various contexts, such as components, composables, macros, and server/runtime code.
Global Context
All of these utilities are auto-imported, globally accessible and can be used with application-based context without needing to import them. Additionally, some of them are also exported from the @vuehouse/dl/exports package for broader use. This allows you to leverage these functions even in environments where the global context is not available (such as server code, runtime code, other utilities, etc.). We will indicate which utilities were exported in their respective sections.
Bellow you'll find a comprehensive list of these utilities with a brief descriptions of their functionality. Where applicable, with examples that illustrate on how to use each effectively.
utilAsyncMap
Asynchronously maps over an array of values.
Example:
Utility is especially useful when you need to map over an array of values asynchronously, or perform multiple operations concurrently. It leverages the Promise.all method to wait for all promises to resolve before continuing.
This utility is exported from the module package.
You can use it this way when the global context is not available and utility can't be loaded automatically by Nuxt.
utilBooleanString
Converts a string with boolean-like value to the real Boolean.
Example:
Utility can be helpful when converting boolean values defined in Markdown - which are represented as strings - before passing them to a component; either defined as value="true" or value=true.
This utility is exported from the module package.
You can use it this way when the global context is not available and utility can't be loaded automatically by Nuxt.
utilCastItem
Converts a given value to a specified type using TypeScript assertions.
Example:
Utility might be very useful when you want to narrow down types, especially when dealing with dynamic data sources or complex structures.
This utility is exported from the module package.
You can use it this way when the global context is not available and utility can't be loaded automatically by Nuxt.
utilCollectionCheck
Validates whether a given collection exists in the list of allowed collections.
Example:
Utility can be very useful to check if a given collection exists when working with Nuxt Content collections. The optional validator is updated as a "side effect".
utilCollectionName
Reads the collection name from the current route.
Example:
Utility is useful when you need to determine which content collection the current page belongs to. It extracts the collection name from route parameters, path segments, or query parameters. Useful for building dynamic navigation or content filtering.
utilContentBody
Extracts the body object from a page collection data, retrieved from the queryCollection method.
Example:
Utility unwraps possible Ref values further using the utilObjectParser to normalize the output.
utilContentSeo
Normalizes custom SEO (_seo) and Schema.org (_schemaOrg) metadata into a plain object(s), retrieved from the queryCollection method.
Example:
Utility can be helpful with differently defined _seo data formats - object vs. array, set within the content page Frontmatter section. If you don't need Schema.org metadata, you can pass false as the second argument to skip the normalization.
More about defining custom SEO metadata you can read here.
utilDataBody
Extracts the meta.body from a data-type collection, retrieved from the queryCollection method.
Example:
Utility unwraps possible Ref values further using the utilObjectParser to normalize the output. Internally it's used to handle sharable Markdown components.
utilEmailParser
Extracts a person's data from an email address by parsing the local part.
Example:
Utility extracts user-friendly name information from an email address. It is used in our Newsletter adapters to generate name data for new mailing list subscribers. The parser skips role-based addresses (like admin, support, hello, contact, etc.) and titles (such as ms, mrs, etc.), returning clean data when possible; if a name cannot be reasonably determined, it will return the string Unknown. You can override the placeholder string with the second argument.
Additionally, the utility returns a boolean value indicating whether the email is valid.
This utility is exported from the module package.
You can use it this way when the global context is not available and utility can't be loaded automatically by Nuxt.
utilCSSVariable
Builds a CSS variable string from a --var name.
Example:
Utility can be helpful while defining dynamic variables or multiple variable definitions within the component's script or template sections.
utilLogMessage
Formats a log message with application tag and source.
Example:
Utility is used throughout the application to format log messages in a consistent and informative way. It helps ensure a unified logging style. By default, the tag is taken from dl.meta.tag, but in some internal utilities, the doclayer tag is hardcoded to clearly indicate the source of internals.
This utility is exported from the module package.
You can use it this way when the global context is not available and utility can't be loaded automatically by Nuxt.
utilMDtoHAST
Converts Markdown input into a normalized HAST / Minimark tree.
Example:
Use this utility whenever you need to convert Markdown into an Abstract Syntax Tree (AST) for further processing - such as rendering content or making transformations.
- By default, the function returns a normalized HAST tree, which is a standard structure for representing HTML.
- If you pass
{ toMinimark: true }, the output will be formatted as a Minimark AST, compatible with the Nuxt Content renderer and theDlRenderercomponent. - If you pass
{ withComponents: true }, the output includes a tree where both custom content and native components are represented, using the Comark library to shape the AST. - If you use both
{ withComponents: true, toMinimark: true }together, you'll get a Minimark-like AST that includes rendered components.
This flexibility allows you to choose exactly the format you need, depending on where and how the parsed Markdown will be used.
utilMDtoHTML
Converts Markdown "tags" into HTML.
Example:
Utility is especially useful when rendering Markdown within inline HTML, most often by using the v-html directive. It handles: backtick code, bold, and italic.
This utility is exported from the module package.
You can use it this way when the global context is not available and utility can't be loaded automatically by Nuxt.
utilObjectParser
Converts complex object structures into plain ones.
Example:
Utility widely used across the codebase to ensure consistent and safe object handling. It can convert proxies and complex data structures (Arrays, Maps, Sets) into plain objects, handle stringified JSON(s), finally guard circular referencing. Use it when you need to safely convert various data types into plain objects.
This utility is exported from the module package.
You can use it this way when the global context is not available and utility can't be loaded automatically by Nuxt.
utilPrimitiveWatch
Creates a lightweight watcher for a primitive value(s).
Example:
Utility provides a simple pub/sub wrapper around a single primitive. It works outside Vue component context - useful in module setup, server/runtime code, or anywhere Vue composables are not available.
Listeners are called synchronously when set() runs. If you register twice the same callback, it will run twice.
Not Vue reactivity
Unlike Vue ref or watch, this utility does not integrate with the component lifecycle. Call the function returned from watch() to unsubscribe when cleanup is needed. Always call methods on the returned object (watcher.watch(...)) - destructuring watch can break unsubscribe.
This utility is exported from the module package.
You can use it this way when the global context is not available and utility can't be loaded automatically by Nuxt.
utilSafeImport
Safely imports a module with timeout.
Example:
Use this utility when attempting to dynamically import a module. If the import fails, or takes longer than the configured timeout, it returns null instead of throwing or hanging. Especially useful for dynamic, user-initiated, or optional imports, where reliability and responsiveness are critical. Below you can see the configuration options that are available:
| Option | Default | Description |
|---|---|---|
silent | false | Skips warning logs on failure. |
timeout | - | Max wait before the import is treated as failed. Time in milliseconds. |
source | undefined | Tag for warning output (e.g. module name that calling this utility). |
ts | false | Resolves TypeScript files with Jiti. |
When to enable ts option
Because native import() cannot evaluate TypeScript files (Node does not understands that) set ts option to true when loading any TypeScript files, local .ts modules, or any specifier that depends on package export conditions. Pass JitiOptions instead of true to customize aliases or other Jiti settings for a one-off import.
Simple example would be with loading app.config.ts file:
This utility is exported from the module package.
You can use it this way when the global context is not available and utility can't be loaded automatically by Nuxt.
utilShikiHighlighter
Returns the globally initialized Shiki highlighter instance.
Example:
Utility provides access to the Shiki syntax highlighter instance, which is initialized once and globally, based on the dl.components.DlCode component configuration. Use it to generate highlighted code blocks with support for multiple languages and themes.
New instance of the Shiki highlighter
If you provide a customConfig object, a new instance of the Shiki highlighter will be created with the specified configuration instead of returning the global one. This is useful when you need different highlighting settings for specific use cases and/or single components.
utilSafeHTML
Sanitizes HTML client-side.
This utility is intended to work with the Vue component's template structure only. It contains the Nuxt runtime internals and should not be used outside of the component context.
Example:
Enabled by default
By default, HTML that is loaded dynamically is sanitized on the client side. This is primarily relevant for components using the Vue. Global sanitization is controlled by the sanitizeHTML property - set this to false to disable sanitization everywhere. For new components, you can also disable it locally as needed, which might be helpful if you want to override the global setting.
Server-side sanitization is not performed, assuming that server-rendered data is already secure. Sanitization is also disabled for DlCode component code blocks, since the variety of code data can lead to improper escaping.
Utility might be very helpful when rendering dynamic HTML content from potentially untrusted sources - such as user inputs or external APIs - particularly when using inline HTML inserts with the v-html directive in templates.
utilSlugGen
Generates URL-friendly slugs.
Example:
Utility is used to generate slugs from content paths, such as for the dl.pathMap (used by the DlLink component), and other internal modules. It is useful whenever you need to create URL-friendly strings from arbitrary input.
It supports two modes: simple and path. The path mode handles extensions, camel-case, diacritics, numbered segments, double slashes, trailing index, spaces, and casing.
This utility is exported from the module package.
You can use it this way when the global context is not available and utility can't be loaded automatically by Nuxt.
utilThrottle
Immediate-leading throttle with per-callback state storing.
Example:
Utility uses an internal caching mechanism to store state of each callback, ensuring that every throttled function maintains its own timing and execution context, without leaking. You can use it to limit how frequently a function can be called, such as user's interactions with an UI or events. It's used for copy-to-clipboard functionality in the DlCode component.
You can pass your own pending state to have it updated accordingly, or simply use the pre-built one. The initial timeout is set to 1000ms = 1s, but you can customize it as needed.
This utility is exported from the module package.
You can use it this way when the global context is not available and utility can't be loaded automatically by Nuxt.
utilTWC
Builds Tailwind class(es) string from array or a tuple-builder.
Example:
Utility provides a smart way to define Tailwind CSS classes. It accepts an array of class names, with suggestions provided by the TypeScript compiler - all for better DX. The array is resolved into a final string containing the complete CSS class list. It works alongside a special module, which supplies a union type of all valid Tailwind classes available at runtime. This enhances autocomplete and type safety. As a base piece it's integrated into the defineDoclayerComponentUi macro, helping to build component-level UI configurations.
When used with a resolver callback function, it provides type inference for the final classes string directly at the definition level.
This utility is exported from the module package.
You can use it this way when the global context is not available and utility can't be loaded automatically by Nuxt.
utilUID
Generates a short, unique & random identifier.
Example:
Utility provides fast, dependency-free UID generation. Use it to create unique identifiers like keys, element IDs, or any situation where a distinct value is needed. You can specify both the length and the character set for the UUID. It uses a fast randomization algorithm based on the given character set, making it much more "predictable" in the final result.
utilWarnPatternCheck
Tests if a warning/error message matches known patterns that are safe to ignore them.
Example:
Utility is useful for adding custom or additional warning filters. However, if you override the default warning handlers provided by DocLayer for Vite or Nitro, some warnings that are normally ignored may get logged unnecessarily. To log every suppressed warning, enable dl.dev.debug mode. Keep in mind that this utility is primarily intended to filter out common warnings that have already been well-tested and addressed during the development.
Below you can see the default set of predefined Regex patterns with explanations that are used in DocLayer error/warning handlers: Vue, Vite & Nitro.
This utility is exported from the module package.
You can use it this way when the global context is not available and utility can't be loaded automatically by Nuxt.
utilWindowWidth
Checks if window.innerWidth is at least the given width.
Example:
This utility is exported from the module package.
You can use it this way when the global context is not available and utility can't be loaded automatically by Nuxt.
