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

feat: hide lines in codeblocks starting with # #629

Merged
merged 1 commit into from
Sep 14, 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
58 changes: 44 additions & 14 deletions src/js_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,32 @@ impl JsDoc {
}
}

fn handle_codeblock<'a>(
line: &'a str,
is_codeblock: &mut bool,
) -> Option<&'a str> {
if *is_codeblock && line.starts_with("# ") {
if line.contains("```") {
*is_codeblock = !*is_codeblock;
Some("```")
} else {
None
}
} else {
if line.contains("```") {
*is_codeblock = !*is_codeblock;
}
Some(line)
}
}

impl From<String> for JsDoc {
fn from(value: String) -> Self {
let mut tags = Vec::new();
let mut doc_lines: Option<String> = None;
let mut is_tag = false;
let mut is_codeblock = false;
let mut tag_is_codeblock = false;
let mut current_tag: Option<String> = None;
let mut current_tag_name = "";
for line in value.lines() {
Expand All @@ -48,6 +69,7 @@ impl From<String> for JsDoc {
assert!(current_tag.is_none());
}
if caps.is_some() {
tag_is_codeblock = false;
let current_tag = std::mem::take(&mut current_tag);
if let Some(current_tag) = current_tag {
tags.push(current_tag.into());
Expand All @@ -56,25 +78,33 @@ impl From<String> for JsDoc {
if let Some(caps) = caps {
current_tag_name = caps.get(1).unwrap().as_str();
}
let current_tag = if let Some(current_tag) = &mut current_tag {
current_tag.push('\n');
current_tag
} else {
current_tag = Some(String::new());
current_tag.as_mut().unwrap()
};
// certain tags, we want to preserve any leading whitespace
if matches!(current_tag_name, "example") {
current_tag.push_str(line.trim_end());
} else {
current_tag.push_str(line.trim());
if let Some(line) = handle_codeblock(line, &mut tag_is_codeblock) {
let current_tag = if let Some(current_tag) = &mut current_tag {
current_tag.push('\n');
current_tag
} else {
current_tag = Some(String::new());
current_tag.as_mut().unwrap()
};

// certain tags, we want to preserve any leading whitespace
if matches!(current_tag_name, "example") {
current_tag.push_str(line.trim_end());
} else {
current_tag.push_str(line.trim());
}
}
} else if let Some(doc_lines) = &mut doc_lines {
doc_lines.push('\n');
doc_lines.push_str(line);
if let Some(line) = handle_codeblock(line, &mut is_codeblock) {
doc_lines.push('\n');
doc_lines.push_str(line);
}
} else {
doc_lines = Some(String::new());
doc_lines.as_mut().unwrap().push_str(line);
if line.contains("```") {
is_codeblock = !is_codeblock;
}
}
}
if let Some(current_tag) = current_tag {
Expand Down
202 changes: 202 additions & 0 deletions tests/specs/Jsdoc_codeblock.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
# mod.ts
/**
* A is a class
*
* ```ts
* const bar = "foo";
* console.log(bar);
* ```
*
* Nothing more
*/
export class A {}
/**
* B is an interface
*
* ```ts
* # const bar = "foo";
* console.log(bar);
* ```
*
* Should be
*/
export interface B {}
/**
* C is a function
*
* ```ts
* class Bar {
* #foo = "something";
* }
* console.log(bar);
* ```
*
* Summarised
*/
export function C() {}
/**
* D
*
* ```ts
* const bar = "foo";
* # console.log(bar);```
*/
export function D() {}

# output.txt
Defined in file:///mod.ts:35:1

function C(): void
C is a function

```ts
class Bar {
#foo = "something";
}
console.log(bar);
```

Summarised

Defined in file:///mod.ts:43:1

function D(): void
D

```ts
const bar = "foo";
```

Defined in file:///mod.ts:11:1

class A
A is a class

```ts
const bar = "foo";
console.log(bar);
```

Nothing more


Defined in file:///mod.ts:22:1

interface B
B is an interface

```ts
console.log(bar);
```

Should be



# output.json
[
{
"name": "A",
"isDefault": false,
"location": {
"filename": "file:///mod.ts",
"line": 11,
"col": 0,
"byteIndex": 105
},
"declarationKind": "export",
"jsDoc": {
"doc": "A is a class\n\n```ts\nconst bar = \"foo\";\nconsole.log(bar);\n```\n\nNothing more"
},
"kind": "class",
"classDef": {
"isAbstract": false,
"constructors": [],
"properties": [],
"indexSignatures": [],
"methods": [],
"extends": null,
"implements": [],
"typeParams": [],
"superTypeParams": []
}
},
{
"name": "B",
"isDefault": false,
"location": {
"filename": "file:///mod.ts",
"line": 22,
"col": 0,
"byteIndex": 232
},
"declarationKind": "export",
"jsDoc": {
"doc": "B is an interface\n\n```ts\nconsole.log(bar);\n```\n\nShould be"
},
"kind": "interface",
"interfaceDef": {
"extends": [],
"constructors": [],
"methods": [],
"properties": [],
"callSignatures": [],
"indexSignatures": [],
"typeParams": []
}
},
{
"name": "C",
"isDefault": false,
"location": {
"filename": "file:///mod.ts",
"line": 35,
"col": 0,
"byteIndex": 383
},
"declarationKind": "export",
"jsDoc": {
"doc": "C is a function\n\n```ts\nclass Bar {\n #foo = \"something\";\n}\nconsole.log(bar);\n```\n\nSummarised"
},
"kind": "function",
"functionDef": {
"params": [],
"returnType": {
"repr": "void",
"kind": "keyword",
"keyword": "void"
},
"hasBody": true,
"isAsync": false,
"isGenerator": false,
"typeParams": []
}
},
{
"name": "D",
"isDefault": false,
"location": {
"filename": "file:///mod.ts",
"line": 43,
"col": 0,
"byteIndex": 479
},
"declarationKind": "export",
"jsDoc": {
"doc": "D\n\n```ts\nconst bar = \"foo\";\n```"
},
"kind": "function",
"functionDef": {
"params": [],
"returnType": {
"repr": "void",
"kind": "keyword",
"keyword": "void"
},
"hasBody": true,
"isAsync": false,
"isGenerator": false,
"typeParams": []
}
}
]
Loading