-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from wilsonneto-dev/wn/add-markdown-support
feat: Add markdown support
- Loading branch information
Showing
6 changed files
with
104 additions
and
6 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,47 @@ | ||
import NavigationNode from "../../model/NavigationNode"; | ||
import MarkdownFileToNavParser from "./MarkdownFileToNavParser"; | ||
|
||
const exampleMarkdown = ` | ||
# Title 1 | ||
## Subtitle 1.1 | ||
## Subtitle 1.2 | ||
# Title 2 | ||
## Subtitle 2.1 | ||
### Subsection 2.1.1 | ||
## Subtitle 2.2 | ||
# Title 3 | ||
## Subtitle 3.1 | ||
`; | ||
|
||
describe('MarkdownFileToNavParser', () => { | ||
let parsedRoot: NavigationNode; | ||
|
||
beforeAll(() => { | ||
const markdownParser = new MarkdownFileToNavParser(); | ||
parsedRoot = markdownParser.parse(exampleMarkdown); | ||
console.log(parsedRoot); | ||
}); | ||
|
||
test('should parse the top-level titles properly', () => { | ||
expect(parsedRoot.getChildren().length).toBe(3); | ||
expect(parsedRoot.getChildren()[0].getName()).toBe('Title 1'); | ||
expect(parsedRoot.getChildren()[1].getName()).toBe('Title 2'); | ||
expect(parsedRoot.getChildren()[2].getName()).toBe('Title 3'); | ||
}); | ||
|
||
test('should parse the subtitles properly', () => { | ||
const title1 = parsedRoot.getChildren()[0]; | ||
expect(title1.getChildren().length).toBe(2); | ||
expect(title1.getChildren()[0].getName()).toBe('Subtitle 1.1'); | ||
expect(title1.getChildren()[1].getName()).toBe('Subtitle 1.2'); | ||
|
||
const title2 = parsedRoot.getChildren()[1]; | ||
expect(title2.getChildren().length).toBe(2); | ||
expect(title2.getChildren()[0].getName()).toBe('Subtitle 2.1'); | ||
expect(title2.getChildren()[0].getChildren().length).toBe(1); // Subsection under Subtitle 2.1 | ||
expect(title2.getChildren()[0].getChildren()[0].getName()).toBe('Subsection 2.1.1'); | ||
expect(title2.getChildren()[1].getName()).toBe('Subtitle 2.2'); | ||
}); | ||
}); |
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,42 @@ | ||
import INavigationParserStrategy from "../../model/INavigationParserStrategy"; | ||
import NavigationNode from "../../model/NavigationNode"; | ||
|
||
class MarkdownFileToNavParser implements INavigationParserStrategy { | ||
public parse(text: string): NavigationNode { | ||
const root = new NavigationNode("Markdown Document"); | ||
const parents: NavigationNode[] = []; | ||
parents.push(root); | ||
|
||
const lines = text.split("\n"); | ||
lines.forEach((line, index) => { | ||
const lineNumber = index; | ||
|
||
if (this.isSectionHeader(line)) { | ||
const header = new NavigationNode(this.getName(line), this.getLevel(line), lineNumber); | ||
while (header.getLevel() <= parents.at(-1)!.getLevel()) { | ||
parents.pop(); | ||
} | ||
|
||
parents.at(-1)!.addChild(header); | ||
parents.push(header); | ||
} | ||
}); | ||
|
||
return root; | ||
} | ||
|
||
private isSectionHeader(line: string): boolean { | ||
return line.trim().startsWith("#") && line.trim().split(" ").length > 1; | ||
} | ||
|
||
private getName(line: string): string { | ||
return line.trim().split(" ").slice(1).join(" ").trim(); | ||
} | ||
|
||
private getLevel(line: string): number { | ||
const numberOfSharps = line.trim().split(" ")[0].split('#').length - 1; | ||
return numberOfSharps; // Levels are 0-based for easier hierarchy | ||
} | ||
} | ||
|
||
export default MarkdownFileToNavParser; |