From 68f4e6d7883e4801c6ee7b05f69a69a16cce039a Mon Sep 17 00:00:00 2001
From: sarahschwartz <58856580+sarahschwartz@users.noreply.github.com>
Date: Wed, 11 Oct 2023 14:32:26 -0600
Subject: [PATCH 1/3] add text import, refactor code import
---
docs/guides/docs/installation/index.mdx | 4 +
.../quickstart/building-a-smart-contract.mdx | 16 ++-
src/components/CodeImport.tsx | 22 ----
src/lib/imports.ts | 8 --
src/lib/md-doc.ts | 2 +
src/lib/plugins/code-import.ts | 47 +++----
src/lib/plugins/rehype-code.ts | 9 +-
src/lib/plugins/text-import.ts | 124 ++++++++++++++++++
8 files changed, 168 insertions(+), 64 deletions(-)
delete mode 100644 src/components/CodeImport.tsx
create mode 100644 src/lib/plugins/text-import.ts
diff --git a/docs/guides/docs/installation/index.mdx b/docs/guides/docs/installation/index.mdx
index fa1a82660..44e0f1d76 100644
--- a/docs/guides/docs/installation/index.mdx
+++ b/docs/guides/docs/installation/index.mdx
@@ -17,13 +17,17 @@ This guide covers the following topics:
## Installing Rust
+{/* install_rust:example:start */}
The Fuel toolchain is built on top of the Rust programming language. To install Rust, you can use the `rustup` tool.
+{/* install_rust:example:end */}
Run the following command in your shell; this downloads and runs rustup-init.sh, which in turn downloads and runs the correct version of the `rustup-init` executable for your platform.
+{/* install_rust_command:example:start */}
```console
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```
+{/* install_rust_command:example:end */}
Check the official Rust documentation to get more information on [installing the Rust toolchain](https://www.rust-lang.org/tools/install).
diff --git a/docs/guides/docs/quickstart/building-a-smart-contract.mdx b/docs/guides/docs/quickstart/building-a-smart-contract.mdx
index 2cacbe9df..c8cd91eaa 100644
--- a/docs/guides/docs/quickstart/building-a-smart-contract.mdx
+++ b/docs/guides/docs/quickstart/building-a-smart-contract.mdx
@@ -10,7 +10,21 @@ parent:
## Installation
-Please visit the [installation guide](guides/installation) to install the Fuel toolchain binaries and prerequisites.
+
{content}; -} diff --git a/src/lib/imports.ts b/src/lib/imports.ts index 565b33d64..49cb24a1a 100644 --- a/src/lib/imports.ts +++ b/src/lib/imports.ts @@ -50,19 +50,11 @@ export function getComponents(docSlug: string, isLatest: boolean) { } if (docSlug.includes('guides/')) { - components.CodeImport = loadComponent( - import('~/src/components/CodeImport'), - 'CodeImport' - ); components.TestAction = TestAction; } else if ( docSlug.includes('docs/wallet') || docSlug.includes('docs/latest/wallet') ) { - components.CodeImport = loadComponent( - import('~/src/components/CodeImport'), - 'CodeImport' - ); components.td = TD; components.th = TH; diff --git a/src/lib/md-doc.ts b/src/lib/md-doc.ts index b0d407c97..6c0ebc50c 100644 --- a/src/lib/md-doc.ts +++ b/src/lib/md-doc.ts @@ -9,6 +9,7 @@ import { codeImport as walletCodeImport } from '~/docs/fuels-wallet/packages/doc import { codeExamples as latestCodeExamples } from '~/docs/latest/fuel-graphql-docs/src/lib/code-examples'; import { codeImport as latestWalletCodeImport } from '~/docs/latest/fuels-wallet/packages/docs/src/lib/code-import'; import { codeImport } from '~/src/lib/plugins/code-import'; +import { textImport } from '~/src/lib/plugins/text-import'; import { DOCS_DIRECTORY } from '../config/constants'; import type { Config, DocType, SidebarLinkItem } from '../types'; @@ -197,6 +198,7 @@ export class Doc { plugins = plugins.concat([[latestCodeExamples, { filepath }] as any]); } else if (this.md.slug.includes('guides')) { plugins = plugins.concat([[codeImport, { filepath }] as any]); + plugins = plugins.concat([[textImport, { filepath }] as any]); } return plugins; diff --git a/src/lib/plugins/code-import.ts b/src/lib/plugins/code-import.ts index ed211518d..2d079775e 100644 --- a/src/lib/plugins/code-import.ts +++ b/src/lib/plugins/code-import.ts @@ -9,6 +9,8 @@ import * as prettier from 'prettier'; import type { Root } from 'remark-gfm'; import { visit } from 'unist-util-visit'; +import { getEndCommentType } from './text-import'; + function toAST(content: string) { return acorn.parse(content, { ecmaVersion: 'latest', @@ -43,7 +45,7 @@ function extractLines( } } -type CommentTypes = '' - : commentType === '{/*' - ? ' */}' - : commentType === '/*' - ? ' */' - : ''; + const endCommentType = getEndCommentType(commentType); + for (let i = 0; i < lines.length; i++) { - const g = `${commentType} ANCHOR: ${comment}${endCommentType}`; - const start = - lines[i] === `${commentType} ${comment}:example:start${endCommentType}` || - lines[i] === `${commentType}${comment}:example:start${endCommentType}` || - lines[i] === g; - if (start === true) { + const startLineA = `${commentType}ANCHOR:${comment}${endCommentType}`; + const endLineA = `${commentType}ANCHOR_END:${comment}${endCommentType}`; + const startLineB = `${commentType}${comment}:example:start${endCommentType}`; + const endLineB = `${commentType}${comment}:example:end${endCommentType}`; + const cleanLine = lines[i].replace(/\s+/g, ''); + const start = cleanLine === startLineA || cleanLine === startLineB; + if (start) { lineStart = i + 1; } else { - const x = `${commentType} ANCHOR_END: ${comment}${endCommentType}`; - const end = - lines[i] === `${commentType} ${comment}:example:end${endCommentType}` || - lines[i] === `${commentType}${comment}:example:end${endCommentType}` || - lines[i] === x; - if (end === true) { + const end = cleanLine === endLineA || cleanLine === endLineB; + if (end) { lineEnd = i; } } @@ -151,7 +144,7 @@ function extractTestCase(source: string, testCase: string) { }; } -const ROOT_DIR = path.resolve(__dirname, '../../../../../../../'); +// const ROOT_DIR = path.resolve(__dirname, '../../../../../../../'); export function codeImport() { return function transformer(tree: Root, file: any) { const rootDir = process.cwd(); @@ -224,16 +217,6 @@ export function codeImport() { type: 'mdxJsxAttribute', value: content, }, - { - name: '__filepath', - type: 'mdxJsxAttribute', - value: path.resolve(dirname, file).replace(`${ROOT_DIR}/`, ''), - }, - { - name: '__filename', - type: 'mdxJsxAttribute', - value: path.parse(file).base, - }, { name: '__language', type: 'mdxJsxAttribute', diff --git a/src/lib/plugins/rehype-code.ts b/src/lib/plugins/rehype-code.ts index f49f72f5b..db64e0c23 100644 --- a/src/lib/plugins/rehype-code.ts +++ b/src/lib/plugins/rehype-code.ts @@ -176,6 +176,9 @@ function codeLanguage() { if (lang?.includes('tsx')) { node.properties.className[0] = 'language-typescript'; } + if (lang?.includes('sh')) { + node.properties.className[0] = 'language-sh'; + } }); }; } @@ -254,7 +257,11 @@ function codeImport() { node.type = 'element'; node.tagName = 'pre'; const lang = node.attributes?.find((a: any) => a.name === '__language'); - const code = h('code', { class: lang?.value }, content?.value); + const code = h( + 'code', + { class: lang?.value }, + content?.value.replace(/\r/g, '') + ); node.children = [code]; }); }; diff --git a/src/lib/plugins/text-import.ts b/src/lib/plugins/text-import.ts new file mode 100644 index 000000000..63dfae78a --- /dev/null +++ b/src/lib/plugins/text-import.ts @@ -0,0 +1,124 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ + +import fs from 'node:fs'; +import { EOL } from 'os'; +import path from 'path'; +import { remark } from 'remark'; +import type { Root } from 'remark-gfm'; +import { visit } from 'unist-util-visit'; +import type { Parent } from 'unist-util-visit/lib'; + +import type { CommentTypes } from './code-import'; + +export function getEndCommentType(commentType: string) { + let commentEnd; + switch (commentType) { + case '/*': + commentEnd = '*/'; + break; + case '{/*': + commentEnd = '*/}'; + break; + case '//': + commentEnd = ''; + break; + case ''; + break; + default: + } + return commentEnd; +} + +function extractCommentBlock( + content: string, + comment: string, + commentType: CommentTypes +) { + const lines = content.split(EOL); + const commentEnd = getEndCommentType(commentType); + let lineStart = 1; + let lineEnd = 1; + for (let i = 0; i < lines.length; i++) { + const start = + lines[i].replace(/\s+/g, '') === + `${commentType}${comment}:example:start${commentEnd}`; + if (start === true) { + lineStart = i + 1; + } else { + const end = + lines[i].replace(/\s+/g, '') === + `${commentType}${comment}:example:end${commentEnd}`; + if (end === true) { + lineEnd = i; + } + } + } + + if (lineStart < 0) { + lineStart = 0; + } + if (lineEnd < 0) { + lineEnd = lines.length; + } + + const linesContent = lines.slice(lineStart, lineEnd).join('\n'); + + return linesContent; +} + +interface Options { + filepath: string; +} + +export function textImport(options: Options = { filepath: '' }) { + const rootDir = process.cwd(); + const { filepath } = options; + const dirname = path.relative(rootDir, path.dirname(filepath)); + + return function transformer(tree: Root) { + const nodes: [any, number | null, Parent