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

astro-asciidoc

v0.0.3

Published

Support AsciiDoc pages in Astro

Downloads

36

Readme

Astro AsciiDoc

CI npm

Support AsciiDoc pages in Astro.

Attention: this package hasn't reached v1 yet and breaking changes may be introduced in minor or patch updates!

Features

  • Convert AsciiDoc pages with Asciidoctor.js
  • Support of AsciiDoc includes
  • Hot reloading of pages and includes
  • Access AsciiDoc page attributes in frontmatter props
  • Load AsciiDoc on the server-side
  • Support of Astro layouts
  • Render pages in standalone mode if no layout provided
  • Page outline/TOC is available in props as Astro MarkdownHeadings
  • Provide Asciidoctor converter options
  • Register Asciidoctor extensions and syntax highlighters
  • Runs Asciidoctor in a worker thread to prevent prototype pollution from Opal/Ruby runtime

Usage

Install:

npm i astro-asciidoc

Add integration to the Astro config:

//astro.config.ts

import { defineConfig } from "astro/config";
import asciidoc from "astro-asciidoc";

export default defineConfig({
  integrations: [
    asciidoc({
      options: {
        /* Asciidoctor.js options */
      },
    }),
  ],
});

Write Pages in AsciiDoc

Add .adoc pages to src/pages/ directory:

= Page Title
:layout: ./src/layouts/Main.astro

== Heading

Text paragraph.

include::_include.adoc[]

Use frontmatter and props in the layout:

---
import type { MarkdownHeading } from "astro";
export interface Props {
  title?: string;
  headings?: MarkdownHeading[];
}
const { title = "Astro", headings = [] } = Astro.props;
---

<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>{title}</title>
  </head>
  <body>
    <nav>
      <ul>
        {
          headings.map((h) => (
            <li>
              <a href={`#${h.slug}`}>{h.text}</a>
            </li>
          ))
        }
      </ul>
    </nav>
    <main>
      <slot />
    </main>
  </body>
</html>

Load AsciiDoc Files

It is also possible to load AsciiDoc on the server side to get access to frontmatter and render the document manually.

First, add astro-asciidoc type reference to the src/env.d.ts file:

// src/env.d.ts

/// <reference types="astro/client" />
/// <reference types="astro-asciidoc/dist/types.d.ts" />

Now import the AsciiDoc file normally from any .astro page or framework component:

---
import Layout from "~/layouts/Main.astro";
import * as doc from "./index.adoc";
---

<Layout title={doc.title} headings={doc.headings}>
  <p>written by {doc.frontmatter.asciidoc.author}</p>
  <doc.Content />
  <p>File: {doc.file}</p>
</Layout>

Please note that the default export is the Content component itself. In order to access named exports, import * as doc or import { frontmatter } syntax should be used.

See example project for more details.

Caveats

NOTE: This integration runs Asciidoctor in a worker thread to prevent prototype pollution from the Opal/Ruby runtime. That means that all options that need to be passed to the Asciidoctor converter need to be serializable according to worker threads message passing limitations. In particular it is currently not possible to pass extensions and syntax highlighters as functions. They need to be in separate Javascript modules and passed through via module file path. Writing extensions and syntax highlighters in TypeScript is also currently not possible. See example project for a sample syntax highlighter integration.

For the same reason it is not possible to expose Asciidoctor classes like Document and Section directly in loaded frontmatter. As an alternative one can write an Asciidoctor extension that will extract required data from the document and store it in document attributes. Attributes are available in frontmatter after import.

Alternatives

Depending on your particular needs you might be interested in other similar projects:

  • astro-adoc - load or glob .adoc documents and render them in a provided component;
  • vite-plugin-asciidoc - General AsciiDoc Vite loader without any special Astro integration;
  • rollup-plugin-asciidoc - Genral AsciiDoc Rollup loader without any special Astro integration;

When I last checked, those ran Asciidoctor directly and didn't prevent prototype pollution from the Opal/Ruby runtime.