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

netlify-cms-widget-create-select

v1.1.5

Published

A simple create select widget for netlify-cms which allows creating entries. The create select widget's entries are up to date to the last website build. The create select can be populated by plain text file, json file, RSS feed or HTML page.

Downloads

30

Readme

netlify-cms-widget-create-select

Check out a demo!

A simple create select widget for netlify-cms which allows creating entries. The create select widget's entries are up to date to the last website build. The create select can be populated by plain text file, json file, RSS feed or HTML page.

Install

As an npm package:

npm install --save netlify-cms-widget-create-select
import { CreatableSelectControl, CreatableSelectPreview } from 'netlify-cms-widget-create-select'

CMS.registerWidget('create-select', CreatableSelectControl, CreatableSelectPreview);

Via script tag:

<script src="https://unpkg.com/netlify-cms-widget-create-select@^1.1.0"></script>

<script>
  CMS.registerWidget('create-select', window.CreatableSelectControl, window.CreatableSelectPreview)
</script>

How to use

Integrate with HTML page

If the built page has all the options you want in a specific html page you can scrape the options from that page.
For example let's say https://www.example.com/tags/ contains the following html page.

<!DOCTYPE html>
<html>
<head></head>
<body>
    <ol id="tag-list">
        <li class="tag" data-tag="tag-1">Tag 1</li>
        <li class="tag" data-tag="tag-2">Tag 2</li>
        <li class="tag" data-tag="tag-3">Tag 3</li>
    </ol>
</body>
</html>

You can query the options tag-1, tag-2, tag-3 with the following configuration

fields:
  - name: 'tags'
    label: 'Tags'
    widget: 'create-select'
    mode: 'html'
    url: 'https://www.example.com/tags/'
    query: '#tag-list > .tag'
    attribute: 'data-tag'
    multiple: true

Integrating with sitemap.xml

You can deduce the tags using the sitemap.xml file. For example if you want to match the string https://www.example.com/tags/<tag-value>/ you can do it the following way.

- name: 'sitemap'
    label: 'Sitemap Example'
    widget: 'create-select'
    mode: 'xml'
    url: 'sitemap.xml'
    query: 'loc'
    capture: '(?<=https:\/\/www\.example\.com\/tags\/).+(?=\/\b)'
    multiple: true

Integrate with HUGO RSS feed

Many HUGO website have /tags/index.xml page with the RSS feed of the tags. An example of the structure of such files is:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Tags on Hugo Themes</title>
    <link>https://www.example.com/tags/</link>
    <description>Recent content in Tags on Hugo Themes</description>
    <generator>Hugo -- gohugo.io</generator>
    <atom:link href="https://www.example.com/tags/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>tag1</title>
      <link>https://www.example.com/tags/tag-1/</link>
      <pubDate>Mon, 11 Mar 2019 00:00:00 +0000</pubDate>
      
      <guid>https://www.example.com/tags/tag-1/</guid>
      <description></description>
    </item>
    
    <item>
      <title>tag2</title>
      <link>https://www.example.com/tags/tag-2/</link>
      <pubDate>Mon, 11 Mar 2019 00:00:00 +0000</pubDate>
      
      <guid>https://www.example.com/tags/tag-2/</guid>
      <description></description>
    </item>
  </channel>
</rss>

The RSS feed of the preview page can be used to populate the select widget. This can be done the following way.

fields:
  - name: 'tags'
    label: 'Tags'
    widget: 'create-select'
    mode: 'xml'
    url: 'https://www.example.com/tags/index.xml'
    query: 'item > title'
    multiple: true

Integrate with HUGO using custom output.

Alternativelly you can make a custom output on the page to populate the select widget.

  • Make a custom output adding the following lines in the hugo config.yaml file
    outputFormats:
      Ingredients:
          baseName: ingredients
          isPlainText: true
          mediaType: text/plain
  • add a layouts/_default/single.ingredients.txt template file to generate the select options.
    {{- $scratch := .Scratch -}}
    {{- with .Site.GetPage "/recipes" -}}
      {{- range .Pages -}}
        {{- range .Params.ingredients -}}
          {{- $scratch.Set . true -}}
        {{- end -}}
      {{- end -}}
    {{- end -}}
    {{- range $key, $value := $scratch.Values -}}
    {{- $key -}}{{- "\n" -}}
    {{- end -}}
  • add a markdown file with the followind front-matter. For example content/ingredients.md
    ---
    _build:
      list: false #prevent file to be listed in sitemap.xml
    outputs: [ingredients]
    ---
  • Now when you compile the page with the command hugo there should be a file generated called public/ingredients.txt. The content of the file should be similar to the following:
    olive oil
    salt
    sugar
  • You can add the followind confin in the netlify config.yml to have a select widget query from the ingredients.txt file:
    fields:
      - name: 'ingredients'
        label: 'Ingredients'
        widget: 'create-select'
        url: 'https://www.example.com/ingredients.txt'
        mode: 'plain'
        multiple: true

Configuration

Create-select widgets may have the following properties in addition to the defaults used by netlity.


mode - string - required

Endpoint file type possible values being "html", "xml", "json", "plain".


url - string - required

The URI of the endpoint which to request the file to populate the select options.


query - string - optional

Query to obtain the data points.

  • With xml or html modes the query is a css selector e.g. query: "div.chapter > .title". This field is required.
  • With json mode query is a dot separated chain of attributes e.g. query: "data.config.users". If not specified the root object will be used. Note that the query should give an array type.
  • With plain mode this field will be ignored.

attribute - string - optional

Attribute to use as value for each data points.

  • With xml or html modes this is an attribute of the element e.g. class, id, data-attr. If not sepcified the textContent will be used instead.
  • With mode: json query is a dot separated chain of attributes e.g. query: "name.firstName". If not specified the root object will be used. Note that the query should give a string type.
  • With plain mode this field will be ignored.

filter - string - optional

Regex for whitch the matching data point's value will be used otherwise the data point is filtered out. The filtering happens before the string is captured.


capture - string - optional

Regex for whitch the matching content will be used by the widget. E.g. capture: "(?<=https:\\/\\/www\\.example\\.com\\/tags\\/).+(?=\\/)" to capture tag1 from the value https://www.example.com/tags/tag1/.
If there is no match the data point will be removed.


multiple - boolean - optional Allow multiple entries.


min - number - optional Minimum number of entries.


max - number - optional Maximum number of entries.


Support

For help with this widget, open an issue.