Skip to content

Commit

Permalink
feat: add mdast-util-select-section package
Browse files Browse the repository at this point in the history
Co-authored-by: vanyauhalin <[email protected]>
  • Loading branch information
ZEROM22 and vanyauhalin committed Nov 18, 2024
1 parent cbec10b commit 1b01727
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 51 deletions.
5 changes: 3 additions & 2 deletions packages/jsdoc-declaration/lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import type * as Library from "@onlyoffice/library-declaration"
// eslint-disable-next-line no-duplicate-imports
import * as library from "@onlyoffice/library-declaration"
import * as signature from "@onlyoffice/library-signature"
import {extractSection} from "@onlyoffice/mdast-util-extract-section"
import {firstSentence} from "@onlyoffice/mdast-util-first-sentence"
import {selectSection} from "@onlyoffice/mdast-util-select-section"
import * as Signature from "@onlyoffice/signature"
import {isStringLiteral} from "@onlyoffice/strings"
import languagedetection from "@vscode/vscode-languagedetection"
Expand Down Expand Up @@ -629,9 +629,10 @@ async function declarationNode(dc: Doclet): Promise<[Library.DeclarationNode, ..
// todo: it is a temporary solution.
if (d.description) {
const a = fromMarkdown(d.description)
const r = selectSection("Try It", a)
const r = extractSection("Try It", a)
const b = toMarkdown(r)
if (b) {
d.description = toMarkdown(a)
d.tryIt = b
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jsdoc-declaration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"@onlyoffice/jsdoc": "workspace:^",
"@onlyoffice/library-declaration": "workspace:^",
"@onlyoffice/library-signature": "workspace:^",
"@onlyoffice/mdast-util-extract-section": "workspace:^",
"@onlyoffice/mdast-util-first-sentence": "workspace:^",
"@onlyoffice/mdast-util-select-section": "workspace:^",
"@onlyoffice/signature": "workspace:^",
"@onlyoffice/strings": "workspace:^",
"@types/mdast": "4.0.3",
Expand Down
10 changes: 10 additions & 0 deletions packages/jsdoc-resource-fixtures/fixtures/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class Cart {

/**
* Adds an item to the cart.
*
* ## Try It
*
* var item = addItem(0, 10);
*
* @summary Puts a certain amount of an item in the cart.
* @param {string} itemId - The ID of the item to add.
* @param {number} quantity - How many of the item to add.
Expand Down Expand Up @@ -54,6 +59,11 @@ class Account {

/**
* Adds money to the account.
*
* ## Try It
*
* var result = deposit("0000-0000-0000-0000", 100);
*
* @summary Deposits an amount into the account.
* @param {string} accountId - The ID of the account to deposit to.
* @param {number} depositAmount - How much to deposit.
Expand Down
60 changes: 60 additions & 0 deletions packages/mdast-util-extract-section/lib/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {fromMarkdown} from "mdast-util-from-markdown"
import {toMarkdown} from "mdast-util-to-markdown"
import {test} from "uvu"
import {is} from "uvu/assert"
import {extractSection} from "./main.ts"

const sc: [string, string, string][] = [
["", "", ""],
["", "h", ""],
["h", "h", ""],
["## h", "h", ""],
[j("## h", "a"), "h", "a"],
[j("## H", "a"), "h", "a"],
[j("## h", "a"), " h ", "a"],
[j("## h", "a", "b"), "h", j("a", "b")],
[j("## h", "a", "## h", "b"), "h", "a"],
[j("# h", "a", "## h", "b"), "h", "b"],
]

for (const [a, h, e] of sc) {
test(`extractSection(): selects section '${h}' from '${a}'`, () => {
const x = fromMarkdown(a)
const y = extractSection(h, x)

let z = toMarkdown(y)
if (z.endsWith("\n")) {
z = z.slice(0, -1)
}

is(z, e)
})
}

const rs: [string, string, string][] = [
["## h", "h", ""],
[j("## h", "a", "## D", "d"), "h", j("## D", "d")],
[j("## D", "d", "## h", "a"), "h", j("## D", "d")],
[j("## D", "d", "## h", "a", "## E", "e"), "h", j("## D", "d", "## E", "e")],
]

for (const [a, h, e] of rs) {
test(`extractSection(): removes section '${h}' from '${a}'`, () => {
const x = fromMarkdown(a)

extractSection(h, x)

let z = toMarkdown(x)
if (z.endsWith("\n")) {
z = z.slice(0, -1)
}

is(z, e)
})
}

test.run()

function j(...a: string[]): string {
return a.join("\n\n")
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
import {isHeadingNode, isParentNode} from "@onlyoffice/mdast-util-is-node"
import {type Node, type Root} from "mdast"
import {type Node, type Root, type RootContent} from "mdast"
import {toString} from "mdast-util-to-string"

export function selectSection(h: string, n: Node): Root {
export function extractSection(h: string, n: Node): Root {
const r: Root = {type: "root", children: []}

if (!h || !isParentNode(n)) {
return r
}

h = h.trim().toLocaleLowerCase()

let f = false
const a: RootContent[] = []

let f: -1 | 0 | 1 = -1

for (const c of n.children) {
if (isHeadingNode(c) && c.depth === 2 && f) {
break
if (isHeadingNode(c) && c.depth === 2 && f === 0) {
f = 1
}

if (isHeadingNode(c) && c.depth === 2) {
if (isHeadingNode(c) && c.depth === 2 && f === -1) {
const s = toString(c).toLocaleLowerCase()

if (s === h) {
f = true
f = 0
continue
}
}

if (f) {
if (f === 0) {
r.children.push(c)
continue
}

a.push(c)
}

n.children = a

return r
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@onlyoffice/mdast-util-select-section",
"name": "@onlyoffice/mdast-util-extract-section",
"type": "module",
"private": true,
"main": "lib/main.ts",
Expand Down
36 changes: 0 additions & 36 deletions packages/mdast-util-select-section/lib/main.test.ts

This file was deleted.

6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1b01727

Please sign in to comment.