npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

menus-generator

v1.1.0

Published

HTML menus (ul based)

Downloads

21

Readme

Menus Generator

Menus generation designed in the same way as Forms Generator

Description

The main interface is Menu class, which creates menu definitions. These definitions could be directly rendered to HTML via render method. Or getContent method could be used to get an object suitable as an argument for Jade Menu mixin. In this case HTML will be rendered as a part of Jade template.

Note: Neither i18n nor jade are included in the production dependencies, but rather they are expected by some methods as arguments. Jade should be compatible with version 1.9.0 and i18n with version ~0.5.0 or ~0.8.0.

Identifiers or IDs

Each item should have an ID that is used for several purposes. All IDs should match /^~?[a-zA-Z_][a-zA-Z0-9_]*$/ regular expression. ~ prefix is stripped from actual IDs.

The first one is generating HTML ID attributes. All item IDs are prefixed with a menu ID. So generated HTML ID attributes will look like MenuID-ItemID. Single - is used as a nesting separator, -- is used to separate ID suffixes for additional elements like links.

The second one is generating translation labels for fields. By default translation IDs generation algorithm is the same as the HTML one, but using non-prefixed IDs is allowed. nTP function disables prefixing for a single ID. Prefixing ID with ~ is similar to using nTP function. Also menu class constructor options allow customisation of menu translation IDs generation. HTML ID attributes are not affected by any of these options.

Items definitions

  • items = item-array | items
  • item-array = [ ID , [ url , attributes , items ] ]
  • ID = /^~?[a-zA-Z_][a-zA-Z0-9_]*$/
  • url = string | null
  • attributes = attributes-object | null | attributes-array
  • attributes-array = [ attributes-object , [ attributes-object , attributes-object , attributes-object ] ]

attributes-object with attribute : value pairs is used to set input HTML elements attributes. attributes-array is used to set attributes to the following elements: attributes-array[0] - <li> tag attributes, attributes-array[1] - <a> tag attributes, attributes-array[2] - <span> tag attributes (text labels inside <a>), attributes-array[3] - <ul> tag attributes in nested menus.

HTML and attributes insertion

This operations doesn't alter a menu definition, separating view style operations. As a result a menu can have several custom view renders.

It is possible to insert attributes and HTML elements into generated menus with an object. Object key are the following selectors prefixed by an element HTML ID:

  • ::before insertion before an element
  • ::after insertion after an element
  • ::attributes insertion of element attributes

For ::before and ::after selectors values can be either HTML strings or arrays with mixin name and arguments (up to 9 mixin arguments are supported). Or attribute objects for ::attributes selector. Class attributes are concatenated, preserving classes defined in a menu constructor.

Another way to insert attributes, based on tag/nesting combinations, is a user supplied attrsExtender function. It should return an attributes-object and it recieves the following arguments:

  • tag - string HTML tag name.
  • level - int menu nesting level, the first level is 0.
  • hasSubmenu - bool true if current item has submenu.

IDs can be abbreviated with the help of @ syntax, so selectors will look like @-ItemID::Selector.

Example

Express 4 example application with pure-menu CSS style is in an example directory.

API

Menu(ID, options, attributes, ...items)

Constructor

Throws:

  • Error with a string description on malformed items definitions.

Arguments:

  • ID - string matching /^~?[a-zA-Z_][a-zA-Z0-9_]*$/ regular expression, or result of nTP function.
  • options - object with menu options or null. Fields:
    • i18nNoPrefix - boolean option to turn off prefixes for translation IDs, false by default.
    • i18nMenuID - string with a menu ID, overrides a default menu ID in translations.
  • attributes - object null.
  • ...items - Rest arguments are interpreted as items definitions.

Menu.getContent(i18n, skipCache)

Method [caches results]

Expands menu for i18n locale and caches results.

Arguments:

  • i18n - i18n translation library.
  • skipCache optional - Ignore the current cache and re-expand content.

Returns:

  • object for Jade menu render.

Menu.render(jade, options, i18n, insertions, ...includeJadeFiles)

Method

Renders HTML menu.

Arguments:

  • jade - jade library.
  • options - jade and render options or null. Render options:
    • attrsExtender - function that extends HTML tags attributes.
    • prefixID - string with prefix that is added to all IDs.
    • skipCache - Ignore the current cache and re-expand content.
  • i18n - i18n translation library.
  • insertions optional - object with HTML insertions or null.
  • ...includeJadeFiles optional - The rest arguments are treated as jade files pathnames to include.

Returns:

  • string HTML menu.

__(str)

Function

Wrapper for strings translation via __ function.

Arguments:

  • str - string to translate.

Returns:

object that will be translated with menu.

__n(str, n)

Function

Wrapper for strings translation via __n function.

Arguments:

  • str - string to translate.
  • n - integer.

Returns:

object that will be translated with menu.

nTP(id)

Function

Forces usage of unprefixed IDs for translation.

Arguments:

  • id - string matching /^~?[a-zA-Z_][a-zA-Z0-9_]*$/ regular expression.

Returns:

object that could be used as ID in menu definitions.

includeJade

Constant

Path to Jade mixins file. This file contains Menu mixin which performs HTML rendering.

Jade API

Menu(data, insertions, attrsExtender, prefixID)

Mixin

Renders menu.

Arguments:

  • data - menu data (a result of js Menu.getContent method).
  • insertions optional - object with HTML insertions data or null.
  • attrsExtender optional - function that extends HTML tags attributes. or null.
  • prefixID optional - string with prefix that is added to all IDs or null.