Function bodies 3 total
parseAndCacheTokens function · typescript · L36-L61 (26 LOC)src/lib/utils/parse-and-cache.ts
export function parseAndCacheTokens(
source: string,
options: SvelteMarkdownOptions,
isInline: boolean
): Token[] | TokensList {
// Check cache first - avoids expensive parsing
const cached = tokenCache.getTokens(source, options)
if (cached) {
return cached
}
// Cache miss - parse and store
const lexer = new Lexer(options)
const parsedTokens = isInline ? lexer.inlineTokens(source) : lexer.lex(source)
const cleanedTokens = shrinkHtmlTokens(parsedTokens) as Token[]
if (typeof options.walkTokens === 'function') {
cleanedTokens.forEach(options.walkTokens)
}
// Cache the cleaned tokens for next time
tokenCache.setTokens(source, options, cleanedTokens)
return cleanedTokens
}hashString function · typescript · L40-L51 (12 LOC)src/lib/utils/token-cache.ts
function hashString(str: string): string {
let hash = 2166136261 // FNV offset basis (32-bit)
for (let i = 0; i < str.length; i++) {
hash ^= str.charCodeAt(i)
// FNV prime multiply using bit shifts (faster than multiplication)
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24)
}
// Convert to unsigned 32-bit integer and base36 string
return (hash >>> 0).toString(36)
}getCacheKey function · typescript · L82-L104 (23 LOC)src/lib/utils/token-cache.ts
function getCacheKey(source: string, options: SvelteMarkdownOptions): string {
const sourceHash = hashString(source)
let optionsHash = optionsHashCache.get(options)
if (!optionsHash) {
const seen = new WeakSet<object>()
optionsHash = hashString(
JSON.stringify(options, (_, value) => {
if (typeof value === 'function') {
return value.name || value.toString()
}
if (value && typeof value === 'object') {
if (seen.has(value)) return '[Circular]'
seen.add(value)
}
return value
})
)
optionsHashCache.set(options, optionsHash)
}
return `${sourceHash}:${optionsHash}`
}