Skip to content

Commit

Permalink
Merge pull request #39 from jstransformers/next
Browse files Browse the repository at this point in the history
Release 1.16.0
  • Loading branch information
RobLoach authored Feb 28, 2023
2 parents 058628b + 5803b22 commit b540a91
Show file tree
Hide file tree
Showing 8 changed files with 1,559 additions and 70 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Node.js CI

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm test
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ build/Release

# Dependency management
node_modules
package-lock.json

# Users Environment Variables
.lock-wscript
15 changes: 0 additions & 15 deletions .travis.yml

This file was deleted.

6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v1.16.0: 2023-02-28

- Updated to twig 1.16.0
- Remove xo coding style constraints
- Dropped support for Node < 8.0.0

## v1.7.0: 2020-10-07

- Added name validation when loading extensions
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

[![Build Status](https://img.shields.io/travis/jstransformers/jstransformer-twig/master.svg)](https://travis-ci.org/jstransformers/jstransformer-twig)
[![Coverage Status](https://img.shields.io/codecov/c/github/jstransformers/jstransformer-twig/master.svg)](https://codecov.io/gh/jstransformers/jstransformer-twig)
[![Dependency Status](https://img.shields.io/david/jstransformers/jstransformer-twig/master.svg)](http://david-dm.org/jstransformers/jstransformer-twig)
[![NPM version](https://img.shields.io/npm/v/jstransformer-twig.svg)](https://www.npmjs.org/package/jstransformer-twig)

## Installation
Expand Down
86 changes: 43 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,38 @@ const validPackageName = require('validate-npm-package-name')

const twigRender = Twig.twig

exports.name = 'twig'
exports.outputFormat = 'html'
const loadModuleFilter = (name, filterModule, extendFunctionName) => {
// Validate that we're loading an actual package.
if (!validPackageName(filterModule).validForNewPackages) {
return
}

try {
// Load the filter module.
const out = require(filterModule)

exports.compile = function (input, options) {
// Check if the module is just a function.
if (typeof out === 'function') {
Twig[extendFunctionName](name, out)
} else if (out && (typeof out === 'object')) {
// Perhaps it is an associative array of functions?
for (const outName in out) {
if (typeof out[outName] === 'function') {
Twig[extendFunctionName](outName, out[outName])
}
}
}
} catch (error) {
console.error(error)
}
}

const transformer = {
name: 'twig',
outputFormat: 'html',
}

transformer.compile = function (input, options) {
// Construct the Twig options.
options = options || {}
options.data = input
Expand All @@ -21,20 +49,16 @@ exports.compile = function (input, options) {
// TODO: Make sure the `root` is correct?
if (options.path && typeof options.path !== 'string') {
const pathRoot = options.root || options.path.root
if (pathRoot) {
options.path = path.join(pathRoot, path.format(options.path))
} else {
options.path = path.format(options.path)
}
options.path = pathRoot ? path.join(pathRoot, path.format(options.path)) : path.format(options.path)
}

// Extend Filters and Functions
const extendable = {
filters: 'extendFilter',
functions: 'extendFunction'
functions: 'extendFunction',
}
// eslint-disable-next-line guard-for-in
for (const extendableName in extendable) {

for (const extendableName of Object.keys(extendable)) {
const extendFunctionName = extendable[extendableName]
// Allow options.filters to be a require() string.
if (typeof options[extendableName] === 'string') {
Expand All @@ -46,38 +70,12 @@ exports.compile = function (input, options) {
}

// Loop through all the given filters.
for (const name in options[extendableName] || {}) {
if ({}.hasOwnProperty.call(options[extendableName], name)) {
switch (typeof options[extendableName][name]) {
case 'string':
// Validate that we're loading an actual package.
if (validPackageName(options[extendableName][name]).validForNewPackages) {
try {
// Load the filter module.
const out = require(options[extendableName][name])

// Check if the module is just a function.
if (typeof out === 'function') {
Twig[extendFunctionName](name, out)
} else if (out && (typeof out === 'object')) {
// Perhaps it is an associative array of functions?
for (const outName in out) {
if (typeof out[outName] === 'function') {
Twig[extendFunctionName](outName, out[outName])
}
}
}
} catch (error) {
console.error(error)
}
}

break
case 'function':
default:
Twig[extendFunctionName](name, options[extendableName][name])
break
}
for (const name of Object.keys(options[extendableName] || {})) {
const filter = options[extendableName][name]
if (typeof filter === 'string') {
loadModuleFilter(name, options[extendableName][name], extendFunctionName)
} else if (typeof filter === 'function') {
Twig[extendFunctionName](name, options[extendableName][name])
}
}
}
Expand All @@ -97,3 +95,5 @@ exports.compile = function (input, options) {

return output
}

module.exports = transformer
Loading

0 comments on commit b540a91

Please sign in to comment.