wingsuit-designsystem / wingsuit

Twig for Storybook
GNU General Public License v2.0
93 stars 16 forks source link

Adding custom Twing filters #284

Open jowan opened 8 months ago

jowan commented 8 months ago

I have some custom Twing filters that correspond to Twig filters in a custom Drupal module. I am including these using a patch to load them in to environment.js duplicating the existing method. Is there a better way to do this ? it's not so bad, but it is a hack.

File @wingsuit-designsystem/preset-twing/dist/environment.js

Including my filters:

var twingFilters = require('twing-drupal-filters');
var myTwingFilters = require('../../../../custom_modules/my-twing-filters');

loading them to the environment:

function init() {
  twingFilters(environment);
  myTwingFilters(environment);
jowan commented 7 months ago

a better way that is - other than duplicating the preset and making my own with this one change.

ademarco commented 5 months ago

We managed in the following way, in your preview.js add:

import { TwingRenderer } from "@wingsuit-designsystem/pattern";
import { TwingFilter } from "twing";

const renderer = new TwingRenderer();
const environment = renderer.getEnvironment();
const foo = (value) => `${value} foo`;
environment.addFilter(
  new TwingFilter("foo", (value) => Promise.resolve(foo(value)))
);

configure(
  ...
  renderer
);