utils.ts — astro Source File
Architecture documentation for utils.ts, a typescript file in the astro codebase. 2 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR b7d87f1f_ca8d_8b9e_5f88_274f48fab2cf["utils.ts"] f76e2597_e3e8_c502_d293_a666b44d49ce["hast"] b7d87f1f_ca8d_8b9e_5f88_274f48fab2cf --> f76e2597_e3e8_c502_d293_a666b44d49ce 57045b72_8cbd_6e93_7687_5176cbcd502d["magic-string"] b7d87f1f_ca8d_8b9e_5f88_274f48fab2cf --> 57045b72_8cbd_6e93_7687_5176cbcd502d style b7d87f1f_ca8d_8b9e_5f88_274f48fab2cf fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import type { Element } from 'hast';
import type MagicString from 'magic-string';
const splitAttrsTokenizer = /([${}@\w:\-]*)\s*=\s*?(['"]?)(.*?)\2\s+/g;
export function replaceAttribute(s: MagicString, node: Element, key: string, newValue: string) {
splitAttrsTokenizer.lastIndex = 0;
const text = s.original
.slice(node.position?.start.offset ?? 0, node.position?.end.offset ?? 0)
.toString();
const offset = text.indexOf(key);
if (offset === -1) return;
const start = node.position!.start.offset! + offset;
const tokens = text.slice(offset).split(splitAttrsTokenizer);
const token = tokens[0].replace(/([^>])>[\s\S]*$/gm, '$1');
if (token.trim() === key) {
const end = start + key.length;
return s.overwrite(start, end, newValue, { contentOnly: true });
} else {
const length = token.length;
const end = start + length;
return s.overwrite(start, end, newValue, { contentOnly: true });
}
}
// Embedding in our own template literal expression requires escaping
// any meaningful template literal characters in the user's code!
const NEEDS_ESCAPE_RE = /[`\\]|\$\{/g;
export function needsEscape(value: any): value is string {
// Reset the RegExp's global state
NEEDS_ESCAPE_RE.lastIndex = 0;
return typeof value === 'string' && NEEDS_ESCAPE_RE.test(value);
}
export function escapeTemplateLiteralCharacters(value: string) {
// Reset the RegExp's global state
NEEDS_ESCAPE_RE.lastIndex = 0;
let char: string | undefined;
let startIndex = 0;
let segment = '';
let text = '';
// Rather than a naive `String.replace()`, we have to iterate through
// the raw contents to properly handle existing backslashes
while (([char] = NEEDS_ESCAPE_RE.exec(value) ?? [])) {
// Final loop when char === undefined, append trailing content
if (!char) {
text += value.slice(startIndex);
break;
}
const endIndex = NEEDS_ESCAPE_RE.lastIndex - char.length;
const prefix = segment === '\\' ? '' : '\\';
segment = prefix + char;
text += value.slice(startIndex, endIndex) + segment;
startIndex = NEEDS_ESCAPE_RE.lastIndex;
}
return text;
}
Domain
Subdomains
Dependencies
- hast
- magic-string
Source
Frequently Asked Questions
What does utils.ts do?
utils.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in utils.ts?
utils.ts defines 3 function(s): escapeTemplateLiteralCharacters, needsEscape, replaceAttribute.
What does utils.ts depend on?
utils.ts imports 2 module(s): hast, magic-string.
Where is utils.ts in the architecture?
utils.ts is located at packages/astro/src/vite-plugin-html/transform/utils.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/vite-plugin-html/transform).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free