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

@pretext-book/fragment

v1.0.0-b7

Published

Take a PreTeXt fragment and create a document suitable for compilation

Downloads

2

Readme

@pretext-book/fragment

Take a PreTeXt fragment and embed it in a pretext document. Then, extract the fragment from the compiled output.

This can be used to make unit tests for PreTeXt implementations.

Fragments and Templates

A PreTeXt fragment looks like the following:

<FRAGMENT parents="chapter section[foo='bar']" template="article">
    <p>Some text and <m>math</m></p>
</FRAGMENT>

It represents a small piece of isolated PreTeXt code that will be inserted into a larger document (a template). The parents attribute is a CSS selector that determines what parent nodes will be dynamically created before being inserted into the template. For the fragment above, the code that would be inserted into the template is

<chapter>
    <section foo="bar">
        <p>Some text and <m>math</m></p>
    </section>
</chapter>

A template is a PreTeXt document with a single <FRAGMENT /> tag in it. For example

<?xml version="1.0" encoding="UTF-8" ?>
<pretext>
    <article>
        <FRAGMENT />
    </article>
</pretext>

When substituting into the template, the <FRAGMENT /> tag is replaced with the content of a PreTeXt fragment.

Installation

Make sure you have nodejs (>= v18) installed.

To install the fragment cli globally, run

npm install @pretext-book/fragment -g

To make a local installation, you'll need a directory with a package.json file. (One can be created for you by running npm init and answering the prompts.) Then run

npm install @pretext-book/fragment

Usage

If you installed globally (and you've configured your path to include npm-install-executables), you should have access to the pretext-fragment command. Otherwise, npx pretext-fragment should execute the fragment cli.

The fragment cli can be used to expand a template and to extract the contents of a template that has been rendered by PreTeXt. For example, if you have @pretext-book/fragment installed locally, you can run

npx pretext-fragment --fragment-file node_modules/@pretext-book/fragment/tests/fragments/p1.xml \
                     --template-file node_modules/@pretext-book/fragment/tests/templates/article.xml \
                     --out sample-main.ptx

sample-main.ptx should now contain a fully-expanded template ready to be compiled to HTML by PreTeXt.

Once the code is compiled, run

npx pretext-fragment --extract-from-html-file node_modules/@pretext-book/fragments/tests/rendered-fragments/p1.html \
                     --out p1-extracted.html

to extract the rendered portion of the fragment.

For more information on the fragment cli, run npx pretext-fragment --help.

Limitations

PreTeXt doesn't always leave clues about what PreTeXt elements render to what HTML. So, @pretext-book/fragment uses a heuristic: it applies unique and ordered ids to all parent elements of the fragment and then extracts the contents of the element with the smallest id in the output source. This means, if the inserted template has auto-generated siblings in the html output, those siblings will also show up in the extracted html.

Development

To develop, follow the instructions in the root directory of the @pretext-book monorepo.