Skip to content

Commit

Permalink
fix: do not render empty lines as files without name (#5)
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
lask79 committed Feb 4, 2024
1 parent aad6825 commit 4daf9ca
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 45 deletions.
2 changes: 1 addition & 1 deletion lib/converter/treeline-to-html-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class TreeLineToHtmlConverter {

function convertLine (node, prefix = '', isLast = true, theme) {
// Base case for empty or undefined node
if (!node) {
if (!node || node.name === '') {
return createLineElement('')
}

Expand Down
101 changes: 58 additions & 43 deletions tests/extension.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,24 @@ README.adoc
expect(html).toContain(example3Result)
})
})
})

describe('When is registered', () => {
it('should convert README.adoc 1', () => {
const source = `.Test
describe('When is registered', () => {
it('should convert README.adoc 1', () => {
const source = `.Test
[treeview]
----
README.adoc
----`

const registry = asciidoctor.Extensions.create()
asciidoctorTreeView.register(registry)
const html = asciidoctor.convert(source, { extension_registry: registry })
const registry = asciidoctor.Extensions.create()
asciidoctorTreeView.register(registry)
const html = asciidoctor.convert(source, { extension_registry: registry })

expect(html).toInclude('<span class="tv-line-element"><img src="https://cdn.jsdelivr.net/npm/material-icon-theme@4.32.0/icons/asciidoc.svg" alt="asciidoc.svg"><span class="tv-item-name">README.adoc</span></span>')
})
expect(html).toInclude('<span class="tv-line-element"><img src="https://cdn.jsdelivr.net/npm/material-icon-theme@4.32.0/icons/asciidoc.svg" alt="asciidoc.svg"><span class="tv-item-name">README.adoc</span></span>')
})

it('should convert bigger structure 1', () => {
const source = `.Test
it('should convert bigger structure 1', () => {
const source = `.Test
[treeview]
----
.
Expand All @@ -78,16 +77,16 @@ README.adoc
----
`

const registry = asciidoctor.Extensions.create()
asciidoctorTreeView.register(registry)
const html = asciidoctor.convert(source, { extension_registry: registry })
// console.log(html)
const registry = asciidoctor.Extensions.create()
asciidoctorTreeView.register(registry)
const html = asciidoctor.convert(source, { extension_registry: registry })
// console.log(html)

// expect(html).to.contain('<span class="emoji"><img src="https://cdn.jsdelivr.net/npm/twemoji@latest/2/svg/1f604.svg" alt="smile" width="24px" height="24px"></span>')
})
})

it('should convert bigger structure 2', () => {
const source = `.Test
it('should convert bigger structure 2', () => {
const source = `.Test
[treeview,asciitree]
----
.
Expand Down Expand Up @@ -136,44 +135,44 @@ README.adoc
----
`

const registry = asciidoctor.Extensions.create()
asciidoctorTreeView.register(registry)
const html = asciidoctor.convert(source, { extension_registry: registry })
const registry = asciidoctor.Extensions.create()
asciidoctorTreeView.register(registry)
const html = asciidoctor.convert(source, { extension_registry: registry })

// expect(html).to.contain('<span class="emoji"><img src="https://cdn.jsdelivr.net/npm/twemoji@latest/2/svg/1f604.svg" alt="smile" width="24px" height="24px"></span>')
})
})

it('should convert to folder if a / is used at the end', () => {
const source = `[treeview]
it('should convert to folder if a / is used at the end', () => {
const source = `[treeview]
├── converter/
└── converter
`

const registry = asciidoctor.Extensions.create()
asciidoctorTreeView.register(registry)
const html = asciidoctor.convert(source, { extension_registry: registry })
// console.log(html)
const registry = asciidoctor.Extensions.create()
asciidoctorTreeView.register(registry)
const html = asciidoctor.convert(source, { extension_registry: registry })
// console.log(html)

expect(html).toInclude('<span class="tv-line-element"><img src="https://cdn.jsdelivr.net/npm/material-icon-theme@4.32.0/icons/folder-open.svg" alt="folder-open.svg"><span class="tv-item-name">converter/</span></span>')
expect(html).toInclude('<span class="tv-line-element"><img src="https://cdn.jsdelivr.net/npm/material-icon-theme@4.32.0/icons/file.svg" alt="file.svg"><span class="tv-item-name">converter</span></span>')
expect(html).toInclude('<span class="tv-line-element"><img src="https://cdn.jsdelivr.net/npm/material-icon-theme@4.32.0/icons/folder-open.svg" alt="folder-open.svg"><span class="tv-item-name">converter/</span></span>')
expect(html).toInclude('<span class="tv-line-element"><img src="https://cdn.jsdelivr.net/npm/material-icon-theme@4.32.0/icons/file.svg" alt="file.svg"><span class="tv-item-name">converter</span></span>')
// expect(html).to.contain('<span class="emoji"><img src="https://cdn.jsdelivr.net/npm/twemoji@latest/2/svg/1f604.svg" alt="smile" width="24px" height="24px"></span>')
})
})

it('should render full page', async () => {
const registry = asciidoctor.Extensions.create()
asciidoctorTreeView.register(registry)
it('should render full page', async () => {
const registry = asciidoctor.Extensions.create()
asciidoctorTreeView.register(registry)

const input = path.join(__dirname, 'inputs/index.adoc')
const output = path.join(__dirname, 'inputs/index.html')
const input = path.join(__dirname, 'inputs/index.adoc')
const output = path.join(__dirname, 'inputs/index.html')

asciidoctor.convertFile(input, { to_file: false, extension_registry: registry, standalone: true, safe: 'safe', attributes: { linkcss: false } })
})
asciidoctor.convertFile(input, { to_file: false, extension_registry: registry, standalone: true, safe: 'safe', attributes: { linkcss: false } })
})

it('render custom symbol', async () => {
const registry = asciidoctor.Extensions.create()
asciidoctorTreeView.register(registry)
it('render custom symbol', async () => {
const registry = asciidoctor.Extensions.create()
asciidoctorTreeView.register(registry)

const source = `
const source = `
.Uses Symbol #
[treeview,symbol="-"]
----
Expand All @@ -199,6 +198,22 @@ root1
- test.hcl
----`

const html = asciidoctor.convert(source, { extension_registry: registry })
const html = asciidoctor.convert(source, { extension_registry: registry })
})

it('should not render icon in empty line', async () => {
const registry = asciidoctor.Extensions.create()
asciidoctorTreeView.register(registry)

const source = `
.Uses Symbol #
[treeview,symbol="-"]
----
----`

const html = asciidoctor.convert(source, { extension_registry: registry })
expect(html).not.toInclude('<span class="tv-line-element"><img src="https://cdn.jsdelivr.net/npm/material-icon-theme@4.32.0/icons/file.svg" alt="file.svg"><span class="tv-item-name"></span></span>')
})
})
})
8 changes: 8 additions & 0 deletions tests/inputs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,11 @@ README.adoc
----
<1> asdasd
|===

.Contains empty line
[treeview]
----
.
# .vscode
----
9 changes: 8 additions & 1 deletion tests/inputs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -752,12 +752,19 @@ <h2 id="_test">Test</h2>
</tr>
</tbody>
</table>
<div class="listingblock treeview dark">
<div class="title">Contains empty line</div>
<div class="content">
<pre><span class="tv-line"><span class="tv-line-prefix"></span><span class="tv-line-element"><img src="https://cdn.jsdelivr.net/npm/material-icon-theme@4.32.0/icons/folder-open.svg" alt="folder-open.svg"><span class="tv-item-name">.</span></span></span>
<span class="tv-line"><span class="tv-line-prefix">└──&nbsp;</span><span class="tv-line-element"><img src="https://cdn.jsdelivr.net/npm/material-icon-theme@4.32.0/icons/file.svg" alt="file.svg"><span class="tv-item-name">.vscode</span></span></span></pre>
</div>
</div>
</div>
</div>
</div>
<div id="footer">
<div id="footer-text">
Last updated 2024-01-17 23:44:44 +0100
Last updated 2024-02-04 13:50:48 +0100
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.3/highlight.min.js"></script>
Expand Down

0 comments on commit 4daf9ca

Please sign in to comment.