Skip to content

Commit

Permalink
refactor: Convert to TypeScript, PNPM, add tsup, update configs (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
AVGVSTVS96 authored Aug 11, 2024
1 parent d559774 commit 20b015b
Show file tree
Hide file tree
Showing 14 changed files with 3,610 additions and 2,522 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules/
.DS_Store
dist/
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.defaultFormatter": "biomejs.biome"
}
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.1.0] - 2024-08-09
### Changed
- Converted to TypeScript [PR #2](https://github.com/avgvstvs96/remark-sectionize/pull/2) by @AVGVSTVS96
- Converted to PNPM [PR #2](https://github.com/avgvstvs96/remark-sectionize/pull/2) by @AVGVSTVS96
- Add Biome for linting and formatting [PR #2](https://github.com/avgvstvs96/remark-sectionize/pull/2) by @AVGVSTVS96
- Update package.json [PR #2](https://github.com/avgvstvs96/remark-sectionize/pull/2) by @AVGVSTVS96

## [2.0.0] - 2023-04-17
### Changed
- Use ESM instead of CommonJS (thanks @Dahmon for the [PR](https://github.com/jake-low/remark-sectionize/pull/12))
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

> This is a fork of Jake Low's [remark-sectionize](https://github.com/jake-low/remark-sectionize), modified by adding a remark-sectionize.d.ts file to get rid of the no type declaration file error in my Astro project. I've also added a test for the type declaration file.
> This is an experimental fork of Jake Low's [remark-sectionize](https://github.com/jake-low/remark-sectionize), converted to TypeScript.

# remark-sectionize

This is a [remark](https://github.com/remarkjs/remark) plugin to wrap each
Expand Down
32 changes: 32 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.1/schema.json",
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"suspicious": {
"noExplicitAny": "warn"
},
"complexity": {
"useArrowFunction": "info"
}
}
},
"formatter": {
"enabled": true,
"indentStyle": "space",
"formatWithErrors": true
},
"javascript": {
"formatter": {
"trailingCommas": "es5",
"bracketSameLine": true
}
},
"files": {
"ignore": ["dist", ".astro", "node_modules"]
}
}
46 changes: 0 additions & 46 deletions index.js

This file was deleted.

71 changes: 71 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { findAfter } from "unist-util-find-after";
import { visit, type BuildVisitor } from "unist-util-visit";
import type { Node, Parent } from "unist";
import type { Transformer } from "unified";

const MAX_HEADING_DEPTH = 6;

interface HeadingNode extends Node {
type: "heading";
depth: 1 | 2 | 3 | 4 | 5 | 6;
}

interface SectionNode extends Parent {
type: "section";
depth: number;
data: {
hName: "section";
};
}

type GenericNode = Node | HeadingNode | SectionNode;

function plugin(): Transformer<GenericNode, GenericNode> {
return transform;
}
type Test = (node: GenericNode) => boolean;

function transform(tree: GenericNode): GenericNode {
for (let depth = MAX_HEADING_DEPTH; depth > 0; depth--) {
visit(
tree,
(node): node is HeadingNode =>
node.type === "heading" &&
"depth" in node &&
typeof node.depth === "number" &&
node.depth === depth,
sectionize as unknown as BuildVisitor<GenericNode, Test>
);
}
return tree;
}

function sectionize(node: HeadingNode, index: number, parent: Parent): void {
const start = node;
const startIndex = index;
const depth = start.depth;

const isEnd = (node: GenericNode): boolean =>
(node.type === "heading" && "depth" in node && node.depth <= depth) ||
node.type === "export";
const end = findAfter(parent, start, isEnd);
const endIndex = end ? parent.children.indexOf(end) : parent.children.length;

const between = parent.children.slice(
startIndex,
endIndex > 0 ? endIndex : undefined
);

const section: SectionNode = {
type: "section",
depth: depth,
children: between,
data: {
hName: "section",
},
};

parent.children.splice(startIndex, section.children.length, section);
}

export default plugin;
Loading

0 comments on commit 20b015b

Please sign in to comment.