Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrected mdast #11

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# Remark CoreBC

This Remark plugin, "remark-corebc," transforms Core Blockchain notations into markdown links, enhancing documents with blockchain data integrations. It features ICAN validation, customizable links to blockchain explorers, and formatting options for various blockchain identifiers.
This Remark plugin, "remark-corebc," transforms Core Blockchain notations into Markdown links (when positively checked) and negated text (when negatively checked), enhancing documents with blockchain data integrations. It features ICAN validation, customizable links to blockchain explorers, and formatting options for various blockchain identifiers.

## About Blockchain

Blockchain is a decentralized, distributed ledger technology that records transactions across a network of computers. It provides transparency, security, and immutability for data storage and transfer. Core Blockchain is a blockchain platform that offers a scalable, secure, and efficient infrastructure for decentralized applications and digital assets.

The main aim is to support [ICAN-based](https://cip.coreblockchain.net/sk-SK/cip/cbc/cip-100) blockchains, such as:

- [Core Blockchain](https://coreblockchain.net/)
- Network Mainnet: [BlockIndex](https://blockindex.net/)
- Network Testnet `Devín`: [BlockIndex](https://xab.blockindex.net/)
- Network Enterprise `Koliba`

## Installation

Expand All @@ -18,7 +29,7 @@ yarn add remark-corebc

## Usage

To automatically convert Core Blockchain notations into clickable links and optionally validate ICAN identifiers:
To automatically convert Core Blockchain notations into clickable links or text definition and optionally validate ICAN identifiers:

```typescript
import remark from 'remark';
Expand All @@ -36,7 +47,7 @@ import remarkCorebc from 'remark-corebc';
})();
```

The plugin recognizes notations like [cb1234...@cb] and [!cb1234...@cb], converting them to links and optionally validating ICAN identifiers.
The plugin recognizes notations such as [cb1234...@cb] and [!cb1234...@cb], converting them to links and optionally validating ICAN identifiers, displaying invalid items as text.

## Options

Expand Down
4 changes: 2 additions & 2 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Node } from 'unist';
import { Root } from 'mdast';
interface CorebcOptions {
enableIcanCheck?: boolean;
enableSkippingIcanCheck?: boolean;
Expand All @@ -13,5 +13,5 @@ interface CorebcOptions {
checkBlockHash?: boolean;
debug?: boolean;
}
export default function remarkCorebc(options?: CorebcOptions): (ast: Node) => void;
export default function remarkCorebc(options?: CorebcOptions): (ast: Root) => void;
export {};
17 changes: 7 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ const makeTextNode = (text) => ({
value: text,
});
const makeReferenceLinkNode = (reference, text) => ({
type: 'paragraph',
type: 'linkReference',
identifier: reference,
label: text,
referenceType: 'full',
children: [
{
type: 'text',
value: `[${text}]`,
},
{
type: 'definition',
identifier: reference,
label: text,
url: reference,
value: text,
},
],
});
Expand Down Expand Up @@ -183,8 +180,8 @@ export default function remarkCorebc(options = {}) {
debug: false,
...options,
};
const transformer = (ast) => {
visit(ast, 'text', (node, index, parent) => {
const transformer = (tree) => {
visit(tree, 'text', (node, index, parent) => {
if (!isTextNode(node) || !parent || typeof index !== 'number')
return;
const parentNode = parent;
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "remark-corebc",
"version": "0.1.8",
"version": "0.2.0",
"description": "A Remark plugin to transform Core Blockchain notations into markdown links.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -43,13 +43,14 @@
"unist-util-visit": "^5.0.0"
},
"devDependencies": {
"@types/mdast": "^4.0.3",
"@types/node": "^20.12.7",
"esm": "^3.2.25",
"remark-parse": "^11.0.0",
"remark-stringify": "^11.0.0",
"ts-node": "^10.9.2",
"typescript": "^5.4.5",
"undici-types": "^6.13.0",
"undici-types": "^6.14.1",
"unified": "^11.0.4",
"uvu": "^0.5.6"
},
Expand Down
62 changes: 16 additions & 46 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type Node } from 'unist';
import { Parent, Root, RootContent, Text, Link, LinkReference } from 'mdast';
import { visit } from 'unist-util-visit';
import Ican from '@blockchainhub/ican';

Expand Down Expand Up @@ -53,34 +54,6 @@ interface CorebcOptions {
debug?: boolean;
}

interface ParentNode extends Node {
children: Node[];
}

interface LinkNode extends Node {
type: 'link';
url: string;
title: string | null;
children: Array<TextNode>;
}

interface TextNode extends Node {
type: 'text';
value: string;
}

interface DefinitionNode extends Node {
type: 'definition';
identifier: string;
label: string;
url: string;
}

interface ReferenceLinkNode extends Node {
type: 'paragraph';
children: Array<TextNode | DefinitionNode>;
}

interface Match {
type: 'address' | 'blockNumber' | 'blockHash';
network: string;
Expand All @@ -92,30 +65,27 @@ interface Match {
length: number;
}

const makeLinkNode = (url: string, text: string, title?: string): LinkNode => ({
const makeLinkNode = (url: string, text: string, title?: string): Link => ({
type: 'link',
url,
title: title || null,
children: [{ type: 'text', value: text }],
});

const makeTextNode = (text: string): TextNode => ({
const makeTextNode = (text: string): Text => ({
type: 'text',
value: text,
});

const makeReferenceLinkNode = (reference: string, text: string): ReferenceLinkNode => ({
type: 'paragraph',
const makeReferenceLinkNode = (reference: string, text: string): LinkReference => ({
type: 'linkReference',
identifier: reference,
label: text,
referenceType: 'full',
children: [
{
type: 'text',
value: `[${text}]`,
},
{
type: 'definition',
identifier: reference,
label: text,
url: reference,
value: text,
},
],
});
Expand Down Expand Up @@ -151,7 +121,7 @@ const slugify = (text: string) => text.toString().toLowerCase()
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text

const isTextNode = (node: Node): node is TextNode => {
const isTextNode = (node: Node): node is Text => {
return node.type === 'text';
}

Expand Down Expand Up @@ -202,7 +172,7 @@ const extractMatches = (text: string, options: CorebcOptions): Match[] => {
};

// Function to transform matches into nodes
const transformMatchesIntoNodes = (matches: Match[], options: CorebcOptions): Node[] => {
const transformMatchesIntoNodes = (matches: Match[], options: CorebcOptions): RootContent[] => {
return matches.flatMap(match => {
let network = match.network;
let fullName, link;
Expand Down Expand Up @@ -282,7 +252,7 @@ const transformMatchesIntoNodes = (matches: Match[], options: CorebcOptions): No
* @param options - Options for the CoreBC plugin.
* @returns A transformer for the AST.
*/
export default function remarkCorebc(options: CorebcOptions = {}): (ast: Node) => void {
export default function remarkCorebc(options: CorebcOptions = {}): (ast: Root) => void {
const finalOptions = {
enableIcanCheck: true, // Enable ICAN check for addresses
enableSkippingIcanCheck: true, // Enable skipping ICAN check with "!" sign
Expand All @@ -299,11 +269,11 @@ export default function remarkCorebc(options: CorebcOptions = {}): (ast: Node) =
...options,
};

const transformer = (ast: Node): void => {
visit<Node, 'text'>(ast, 'text', (node: TextNode, index: number, parent: ParentNode | undefined) => {
const transformer = (tree: Root): void => {
visit(tree, 'text', (node: Text, index: number | undefined, parent: Node | undefined) => {
if (!isTextNode(node) || !parent || typeof index !== 'number') return;
const parentNode: ParentNode = parent as ParentNode;
let newNodes: Node[] = [];
const parentNode: Parent = parent as Parent;
let newNodes: RootContent[] = [];
let lastIndex = 0;

const matches = extractMatches(node.value, finalOptions);
Expand Down
Loading