Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Упрощает задание shortcode для blob #337

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 19 additions & 38 deletions eleventy.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,44 +177,25 @@ module.exports = function(config) {

// Теги

config.addNunjucksTag('blob', (nunjucksEngine) => {
return new function() {
this.tags = ['blob'];

this.parse = function(parser, nodes) {
const token = parser.nextToken();

const args = parser.parseSignature(null, true);
parser.advanceAfterBlockEnd(token.value);

return new nodes.CallExtensionAsync(this, 'run', args);
};

this.run = function(context, authorName, callback) {
const blobColors = [1, 2, 3, 4];
const blobShapes = [1, 2, 3, 4, 5, 6, 7];
const shapePrefix = 'blob--shape-';
const colorPrefix = 'blob--color-';

const getBlobClass = (basis, array, name) => (
name.concat(array[basis % array.length])
);

const shapeBasis = authorName.split('').reduce(
(previous, current) => previous + current.charCodeAt(0), 0
);
const colorBasis = authorName.length;

const colorClass = getBlobClass(colorBasis, blobColors, colorPrefix);
const shapeClass = getBlobClass(shapeBasis, blobShapes, shapePrefix);

callback(
null, new nunjucksEngine.runtime.SafeString(
colorClass.concat(' ', shapeClass)
)
);
};
}();
config.addShortcode('blob', function(authorName) {
const blobColors = [1, 2, 3, 4];
const blobShapes = [1, 2, 3, 4, 5, 6, 7];
const shapePrefix = 'blob--shape-';
const colorPrefix = 'blob--color-';

const getBlobClass = (basis, array, name) => (
name.concat(array[basis % array.length])
);

const shapeBasis = authorName.split('').reduce(
(previous, current) => previous + current.charCodeAt(0), 0
);
const colorBasis = authorName.length;

const colorClass = getBlobClass(colorBasis, blobColors, colorPrefix);
const shapeClass = getBlobClass(shapeBasis, blobShapes, shapePrefix);

return colorClass.concat(' ', shapeClass);
});

// Копирование
Expand Down