Giter Site home page Giter Site logo

astro-asciidoc's Introduction

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.

astro-asciidoc's People

Contributors

renovate[bot] avatar shishkin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

advanced-astro

astro-asciidoc's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • chore(deps): update dependency astro to v4.7.1
  • chore(deps): update devdependencies (@commitlint/cli, @typescript-eslint/eslint-plugin, @typescript-eslint/parser, release-it)
  • chore(deps): update dependency eslint-plugin-astro to v1

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/ci.yaml
  • actions/checkout v4
  • actions/setup-node v4
.github/workflows/release.yaml
  • actions/checkout v4
  • actions/setup-node v4
npm
example/package.json
  • astro 4.6.3
  • shiki 0.14.7
  • typescript 5.4.5
package.json
  • @asciidoctor/core 3.0.4
  • @commitlint/cli 19.2.2
  • @commitlint/config-conventional 19.2.2
  • @release-it/conventional-changelog 8.0.1
  • @types/eslint 8.56.10
  • @astrojs/check 0.5.10
  • @types/prettier 2.7.3
  • @typescript-eslint/eslint-plugin 7.7.0
  • @typescript-eslint/parser 7.7.0
  • astro 4.6.3
  • eslint 8.57.0
  • eslint-config-prettier 9.1.0
  • eslint-plugin-astro 0.34.0
  • eslint-plugin-prettier 5.1.3
  • husky 9.0.11
  • prettier 3.2.5
  • prettier-plugin-astro 0.13.0
  • release-it 17.2.0
  • typescript 5.4.5
  • unbuild 2.0.0
  • astro ^2.8.0 || ^3.0.0 || ^4.0.0
  • vite ^4.3.9 || ^5.0.0
nvm
.nvmrc

  • Check this box to trigger a request for Renovate to run again on this repository

Prototype pollution by Opal

It seems that running Asciidoctor.js does pollute Javascript native object prototypes that leads to weird bugs. For example, this code adds a stub $id method to Array prototype so all arrays suddenly get it:

import asciidoctor from "@asciidoctor/core";

asciidoctor();

console.log([].$id);
// [Function: method_missing_stub] { '$$stub': true }
console.log(Array.prototype.$id);
// [Function: method_missing_stub] { '$$stub': true }

I discover a bug with this in ajv schema validator that runs into issues when it encounters an array of schemas with a present $id property, which it only expects to find on a single schema, not array.

Doesn't work in Astro 3 anymore

This integration doesn't with in Astro 3 anymore. Astro refuses to render the generated/transformed AsciiDoc JSX page:

 error   Unable to render `Content`.

  No valid renderer was found for this file extension.

I was unable to troubleshoot the issue and raised it with Astro team a few times (without any response so far):

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Location: renovate.json
Error type: Invalid JSON (parsing failed)
Message: Syntax error: expecting String near onth"], }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.