This repository contains the completed code for a custom Remark plugin integrated into a SvelteKit project. This example accompanies a tutorial on my blog, JimsCode, where I explain how to build and use this plugin step-by-step.
The custom Remark plugin remarkExtractHeaders
performs the following tasks:
Extract Headers:
Add IDs to Nodes:
id
property of each heading node, making it easy to create anchors for each section.Attach Headers Data:
file.data.fm.headers
property of the file object.This repository is intended for testing and running the completed code example. For detailed instructions on building and integrating this plugin into your own SvelteKit project, please refer to the tutorial on my blog: How to Extract Headers from Markdown in SvelteKit with Remark.
To test the completed code, follow these steps:
Clone this repository:
git clone https://github.com/scionsamurai/remark-extract-headers-sveltekit.git
Navigate to the project directory:
cd remark-extract-headers-sveltekit
Install dependencies:
npm install
Run the SvelteKit development server:
npm run dev
Header Extraction:
Generated Data:
file.data.headers
array contains objects with text
, id
, and depth
properties for each header.An example Markdown document processed by this plugin might look like this:
## Introduction
Some introduction text.
### Subsection
Details about the subsection.
## Another Section
Content for another section.
After processing, the header data will be structured as follows:
[
{
"text": "Introduction",
"id": "introduction",
"depth": 2,
"children": [
{
"text": "Subsection",
"id": "subsection",
"depth": 3
}
]
},
{
"text": "Another Section",
"id": "another-section",
"depth": 2
}
]