Skip to content

Commit

Permalink
[aptos-nextra-components] Initialize mdast
Browse files Browse the repository at this point in the history
  • Loading branch information
hariria committed Sep 5, 2024
1 parent 7dec28f commit f60c10b
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 19 deletions.
6 changes: 6 additions & 0 deletions apps/nextra/utils/generated/localeMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,9 @@ export const localeMap = {
"/build/smart-contracts/book/constants": {
"en": true
},
"/build/smart-contracts/bookums": {
"en": true
},
"/build/smart-contracts/book/equality": {
"en": true
},
Expand Down Expand Up @@ -481,6 +484,9 @@ export const localeMap = {
"/build/smart-contracts/book/modules-and-scripts": {
"en": true
},
"/build/smart-contracts/book/move-2.0": {
"en": true
},
"/build/smart-contracts/book/move-tutorial": {
"en": true
},
Expand Down
14 changes: 11 additions & 3 deletions packages/aptos-nextra-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@
"build:watch": "tsup --watch",
"lint:fix": "eslint --ext .ts,.tsx --quiet --fix --ignore-path ./.gitignore",
"lint:format": "prettier --loglevel warn --write \"./**/*.{ts,tsx,css,md,json}\" ",
"lint": "pnpm lint:format && pnpm lint:fix "
"lint": "pnpm lint:format && pnpm lint:fix ",
"test": "vitest"
},
"dependencies": {
"clsx": "2.1.0",
"tailwind-merge": "^2.2.2"
"mdast-util-from-markdown": "^2.0.1",
"mdast-util-to-markdown": "^2.1.0",
"remark-parse": "^11.0.0",
"remark-stringify": "^11.0.0",
"tailwind-merge": "^2.2.2",
"unified": "^11.0.5",
"unist-util-visit": "^5.0.0"
},
"devDependencies": {
"@types/node": "^20.11.17",
Expand All @@ -39,7 +46,8 @@
"react-dom": "^18.2.0",
"rimraf": "6.0.1",
"tsup": "8.2.2",
"typescript": "5.5.3"
"typescript": "5.5.3",
"vitest": "1.3.0"
},
"peerDependencies": {
"nextra-theme-docs": "3.0.0-alpha.24",
Expand Down
105 changes: 105 additions & 0 deletions packages/aptos-nextra-components/src/utils/mdast/examples/account.md
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>&lt;u8&gt;</code>
</dt>
<dd>

</dd>
<dt>
<code>new_authentication_key: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;</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>&lt;u8&gt;</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>&lt;<a href="account.md#0x1_account_CoinRegisterEvent">account::CoinRegisterEvent</a>&gt;</code>
</dt>
<dd>

</dd>
<dt>
<code>key_rotation_events: <a href="event.md#0x1_event_EventHandle">event::EventHandle</a>&lt;<a href="account.md#0x1_account_KeyRotationEvent">account::KeyRotationEvent</a>&gt;</code>
</dt>
<dd>

</dd>
<dt>
<code>rotation_capability_offer: <a href="account.md#0x1_account_CapabilityOffer">account::CapabilityOffer</a>&lt;<a href="account.md#0x1_account_RotationCapability">account::RotationCapability</a>&gt;</code>
</dt>
<dd>

</dd>
<dt>
<code>signer_capability_offer: <a href="account.md#0x1_account_CapabilityOffer">account::CapabilityOffer</a>&lt;<a href="account.md#0x1_account_SignerCapability">account::SignerCapability</a>&gt;</code>
</dt>
<dd>

</dd>
</dl>


</details>
11 changes: 11 additions & 0 deletions packages/aptos-nextra-components/src/utils/mdast/mdast.test.ts
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)
})
58 changes: 58 additions & 0 deletions packages/aptos-nextra-components/src/utils/mdast/mdast.ts
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(/&lt;/g, '<') // Decode escaped characters
.replace(/&gt;/g, '>')
.replace(/&amp;/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)
}
Loading

0 comments on commit f60c10b

Please sign in to comment.