-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[aptos-nextra-components] Initialize mdast
- Loading branch information
Showing
6 changed files
with
311 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
packages/aptos-nextra-components/src/utils/mdast/examples/account.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
|
||
|
||
<a id="0x1_account_KeyRotation"></a> | ||
|
||
## Struct `KeyRotation` | ||
|
||
|
||
|
||
<pre><code>#[<a href="event.md#0x1_event">event</a>] | ||
<b>struct</b> <a href="account.md#0x1_account_KeyRotation">KeyRotation</a> <b>has</b> drop, store | ||
</code></pre> | ||
|
||
|
||
|
||
<details> | ||
<summary>Fields</summary> | ||
|
||
|
||
<dl> | ||
<dt> | ||
<code><a href="account.md#0x1_account">account</a>: <b>address</b></code> | ||
</dt> | ||
<dd> | ||
|
||
</dd> | ||
<dt> | ||
<code>old_authentication_key: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a><u8></code> | ||
</dt> | ||
<dd> | ||
|
||
</dd> | ||
<dt> | ||
<code>new_authentication_key: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a><u8></code> | ||
</dt> | ||
<dd> | ||
|
||
</dd> | ||
</dl> | ||
|
||
|
||
</details> | ||
|
||
<a id="0x1_account_Account"></a> | ||
|
||
## Resource `Account` | ||
|
||
Resource representing an account. | ||
|
||
|
||
<pre><code><b>struct</b> <a href="account.md#0x1_account_Account">Account</a> <b>has</b> store, key | ||
</code></pre> | ||
|
||
|
||
|
||
<details> | ||
<summary>Fields</summary> | ||
|
||
|
||
<dl> | ||
<dt> | ||
<code>authentication_key: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a><u8></code> | ||
</dt> | ||
<dd> | ||
|
||
</dd> | ||
<dt> | ||
<code>sequence_number: u64</code> | ||
</dt> | ||
<dd> | ||
|
||
</dd> | ||
<dt> | ||
<code>guid_creation_num: u64</code> | ||
</dt> | ||
<dd> | ||
|
||
</dd> | ||
<dt> | ||
<code>coin_register_events: <a href="event.md#0x1_event_EventHandle">event::EventHandle</a><<a href="account.md#0x1_account_CoinRegisterEvent">account::CoinRegisterEvent</a>></code> | ||
</dt> | ||
<dd> | ||
|
||
</dd> | ||
<dt> | ||
<code>key_rotation_events: <a href="event.md#0x1_event_EventHandle">event::EventHandle</a><<a href="account.md#0x1_account_KeyRotationEvent">account::KeyRotationEvent</a>></code> | ||
</dt> | ||
<dd> | ||
|
||
</dd> | ||
<dt> | ||
<code>rotation_capability_offer: <a href="account.md#0x1_account_CapabilityOffer">account::CapabilityOffer</a><<a href="account.md#0x1_account_RotationCapability">account::RotationCapability</a>></code> | ||
</dt> | ||
<dd> | ||
|
||
</dd> | ||
<dt> | ||
<code>signer_capability_offer: <a href="account.md#0x1_account_CapabilityOffer">account::CapabilityOffer</a><<a href="account.md#0x1_account_SignerCapability">account::SignerCapability</a>></code> | ||
</dt> | ||
<dd> | ||
|
||
</dd> | ||
</dl> | ||
|
||
|
||
</details> |
11 changes: 11 additions & 0 deletions
11
packages/aptos-nextra-components/src/utils/mdast/mdast.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { expect, test } from 'vitest' | ||
import { astToMarkdown, convertHtmlToMarkdownCodeBlocks, readFileAsTree } from './mdast' | ||
|
||
test('mdast readFileAsTree from local markdown', () => { | ||
const tree = readFileAsTree(); | ||
console.log("Tree pre-treatment: ", tree); | ||
convertHtmlToMarkdownCodeBlocks(tree); | ||
console.log("Tree post-codeblock treatment: ", tree); | ||
const markdown = astToMarkdown(tree); | ||
console.log(markdown) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
import { fromMarkdown } from 'mdast-util-from-markdown' | ||
import { toMarkdown } from "mdast-util-to-markdown"; | ||
import { Code, InlineCode, Root, Text } from "mdast-util-from-markdown/lib"; | ||
import { visit } from "unist-util-visit"; | ||
|
||
/** | ||
* Read File as Tree | ||
*/ | ||
export function readFileAsTree(relativePath: string): Root { | ||
const localPath = path.resolve(__dirname, relativePath); | ||
const doc = fs.readFileSync(localPath) | ||
const tree = fromMarkdown(doc) | ||
return tree; | ||
} | ||
|
||
/** | ||
* Convert HTML to markdown code blocks | ||
* | ||
* Parser first pass | ||
*/ | ||
export function convertHtmlToMarkdownCodeBlocks(tree: Root) { | ||
visit(tree, 'html', (node, index, parent) => { | ||
// Codeblock parsing (only applies to <pre><code>) | ||
const codeblockRegex = /<pre><code[^>]*>([\s\S]*?)<\/code><\/pre>/; | ||
const codeblockMatch = codeblockRegex.exec(node.value); | ||
|
||
if (codeblockMatch) { | ||
// Extract the content between <code> and </code> | ||
const codeContent = codeblockMatch[1] | ||
.replace(/<[^>]+>/g, '') // Remove all HTML tags | ||
.replace(/</g, '<') // Decode escaped characters | ||
.replace(/>/g, '>') | ||
.replace(/&/g, '&') | ||
.trim(); | ||
|
||
// Create a new 'code' node | ||
const codeNode: Code = { | ||
type: 'code', | ||
lang: 'move', // You can adjust or detect the language dynamically | ||
meta: null, | ||
value: codeContent, | ||
position: node.position, // Preserve the original position | ||
}; | ||
|
||
// Replace the current 'html' node with the new 'code' node | ||
if (parent && index !== undefined) { | ||
parent.children[index] = codeNode; | ||
return; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
export const astToMarkdown = (tree: Root) => { | ||
return toMarkdown(tree) | ||
} |
Oops, something went wrong.