Skip to content

feat: add support for custom tag types - #368

Merged
bgub merged 4 commits into
mainfrom
Tikolu/main
Apr 25, 2026
Merged

feat: add support for custom tag types#368
bgub merged 4 commits into
mainfrom
Tikolu/main

Conversation

@bgub

@bgub bgub commented Apr 25, 2026

Copy link
Copy Markdown
Owner

Summary

  • Adds a customTags config option for defining custom tag prefixes with handler functions
  • Keys are tag prefixes, values are functions that receive the tag content (as a string) and the template data object
  • Supersedes feat: Add suport for custom tag types #364 by @Tikolu — original commit preserved with cleanup on top

Usage

const translations = {
  en: { greeting: "Hello!" },
  pl: { greeting: "Cześć!" },
};

const eta = new Eta({
  customTags: {
    "#": () => "",  // comment tag
    "*": (key, data) => translations[data.lang][key.trim()],
  },
});

eta.renderString("<%# comment %><p><%* greeting %></p>", { lang: "en" });
// => "<p>Hello!</p>"

Changes

  • src/config.ts — add customTags to EtaConfig with default {}
  • src/parse.ts — include custom tag prefixes in the parser regex, set tag type to the prefix string
  • src/compile-string.ts — dispatch custom tag types in compileBody via this.config.customTags
  • test/render.spec.ts — 4 tests (basic lookup, comment, multiple tags, mixed with built-in tags)

Test plan

  • Full suite (106 tests) passing
  • pnpm format, pnpm lint, pnpm build all clean

Closes #364

Tikolu and others added 2 commits April 25, 2026 10:55
- Remove unused variable in compileBody
- Use JSON.stringify for safe escaping of tag type and content
- Add missing semicolon in parse.ts
- Restore original ternary style for tag type resolution
- Add tests for custom tags
@codecov

codecov Bot commented Apr 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.77778% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.35%. Comparing base (acff516) to head (299f09e).

Files with missing lines Patch % Lines
src/compile-string.ts 66.66% 0 Missing and 1 partial ⚠️
src/parse.ts 50.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #368      +/-   ##
==========================================
+ Coverage   92.15%   92.35%   +0.20%     
==========================================
  Files          11       11              
  Lines         293      301       +8     
  Branches       78       81       +3     
==========================================
+ Hits          270      278       +8     
  Misses          9        9              
  Partials       14       14              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

bgub added 2 commits April 25, 2026 11:13
- Use Object.hasOwn instead of `in` operator for custom tag lookup
- Throw on custom tag prefixes that conflict with built-in prefixes or trim markers
@bgub
bgub merged commit 5105744 into main Apr 25, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants