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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ncbijs/jats

v0.0.0

Published

Parser for JATS XML full-text articles with RAG chunking

Downloads

135

Readme


Why

JATS (Journal Article Tag Suite) is the standard XML format for full-text articles in PMC. It's deeply nested with complex section structure, inline formulas, tables, figures, and references. Extracting usable text — especially for RAG pipelines that need properly chunked content with section metadata — requires understanding the full spec. This package parses once and outputs in the format you need.

  • Structured parse — front matter, body sections, back matter (references, acknowledgements, appendices)
  • Markdown output — headings, tables, figures, references formatted as markdown
  • Plain-text output — clean text with indentation for section depth
  • RAG chunking — split body into token-limited chunks with section paths and overlap control

Install

npm install @ncbijs/jats

Quick start

Parse and convert to markdown

import { parseJATS, toMarkdown } from '@ncbijs/jats';

const xml = await fetch(
  'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pmc&id=PMC1234567',
).then((r) => r.text());

const article = parseJATS(xml);
const markdown = toMarkdown(article);
console.log(markdown);

Extract plain text

import { parseJATS, toPlainText } from '@ncbijs/jats';

const article = parseJATS(xml);
const text = toPlainText(article);

RAG chunking

import { parseJATS, toChunks } from '@ncbijs/jats';

const article = parseJATS(xml);
const chunks = toChunks(article, { maxTokens: 256 });

for (const chunk of chunks) {
  console.log(chunk.section, chunk.tokenCount);
  console.log(chunk.text);
}

API

parseJATS(xml)

Parse a JATS XML string into a structured article object with front matter, body sections, and back matter.

const article = parseJATS(xmlString);
console.log(article.front.article.title);
console.log(article.body.length); // number of top-level sections
console.log(article.back.references.length);

| Parameter | Type | Description | | --------- | -------- | ---------------- | | xml | string | JATS XML string. |

Returns JATSArticle.

Throws if the input is empty or contains no <article> element.

toMarkdown(article)

Convert a parsed article to markdown. Produces headings for sections (depth-aware), markdown tables, figure captions, and a numbered reference list.

const markdown = toMarkdown(article);

| Parameter | Type | Description | | --------- | ------------- | -------------------- | | article | JATSArticle | Parsed JATS article. |

Returns string.

toPlainText(article)

Extract plain text from a parsed article. Sections are indented by depth. Tables show rows separated by pipes. Figures are shown as bracketed labels.

const text = toPlainText(article);

| Parameter | Type | Description | | --------- | ------------- | -------------------- | | article | JATSArticle | Parsed JATS article. |

Returns string.

toChunks(article, options?)

Split the article body into token-limited chunks suitable for RAG pipelines. Each chunk carries its section title and approximate token count. Adjacent chunks overlap to preserve context at boundaries.

const chunks = toChunks(article);
const smallChunks = toChunks(article, { maxTokens: 256, overlap: 30 });

| Parameter | Type | Required | Description | | --------- | -------------- | -------- | ----------------------- | | article | JATSArticle | Yes | Parsed JATS article. | | options | ChunkOptions | No | Chunking configuration. |

Returns ReadonlyArray<Chunk>.

ChunkOptions

| Option | Type | Default | Description | | --------------------- | --------- | ------- | -------------------------------------------------------- | | maxTokens | number | 512 | Approximate token limit per chunk (word-based estimate). | | overlap | number | 50 | Number of overlapping tokens between adjacent chunks. | | includeSectionTitle | boolean | true | Prepend the section title to each chunk's text. |

Chunk

| Field | Type | Description | | ------------ | ------------------------- | ------------------------------------------------- | | text | string | Chunk text content. | | section | string | Section title this chunk belongs to. | | tokenCount | number | Approximate token count (whitespace-split words). | | metadata | Record<string, unknown> | Additional metadata (includes depth). |

JATSArticle

The top-level parsed structure:

| Field | Type | Description | | ------- | ------------------------ | ---------------------------------------------- | | front | Front | Journal metadata and article metadata. | | body | ReadonlyArray<Section> | Body sections with paragraphs and subsections. | | back | Back | References, acknowledgements, appendices. |

Front

| Field | Type | Description | | --------- | ------------- | ----------------- | | journal | JournalMeta | Journal metadata. | | article | ArticleMeta | Article metadata. |

ArticleMeta

| Field | Type | Description | | ----------------- | ------------------------ | ----------------- | | title | string | Article title. | | authors | ReadonlyArray<Author> | Author list. | | abstract | string (optional) | Abstract text. | | doi | string (optional) | DOI. | | pmid | string (optional) | PubMed ID. | | pmcid | string (optional) | PMC ID. | | publicationDate | PartialDate (optional) | Year, month, day. |

Section

| Field | Type | Description | | ------------- | ------------------------ | ------------------------ | | title | string | Section heading. | | depth | number | Nesting depth (1 = top). | | paragraphs | ReadonlyArray<string> | Paragraph text content. | | tables | ReadonlyArray<Table> | Tables in this section. | | figures | ReadonlyArray<Figure> | Figures in this section. | | subsections | ReadonlyArray<Section> | Nested subsections. |

Reference

| Field | Type | Description | | --------- | ----------------------- | ---------------------------- | | id | string | Reference ID (e.g., ref1). | | label | string (optional) | Display label (e.g., 1). | | authors | ReadonlyArray<string> | Author names. | | title | string | Article title. | | source | string | Journal/source name. | | year | number (optional) | Publication year. | | volume | string (optional) | Volume number. | | pages | string (optional) | Page range. | | doi | string (optional) | DOI. | | pmid | string (optional) | PubMed ID. |

Types

All types are exported for use in your own interfaces:

import type {
  JATSArticle,
  Front,
  Back,
  Section,
  Chunk,
  ChunkOptions,
  Reference,
  Figure,
  Table,
  Author,
  ArticleMeta,
  JournalMeta,
  PartialDate,
} from '@ncbijs/jats';