Skip to content

Commit

Permalink
fix bug in current slide calculation
Browse files Browse the repository at this point in the history
fixes #7
  • Loading branch information
flobilosaurus committed Feb 28, 2020
1 parent 40b241c commit 36d6a40
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [0.4.1]

- fixed: [bug in calculation of slide under cursor ](https://github.com/flobilosaurus/vscode-asciidoc-slides/issues/7)

## [0.4.0]

- add kroki integration, which converts textual description of diagrams to images (see https://github.com/Mogztter/asciidoctor-kroki)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-asciidoc-slides",
"displayName": "AsciiDoc Slides",
"description": "Create a presentation via AsciiDoc",
"version": "0.4.0",
"version": "0.4.1",
"publisher": "flobilosaurus",
"repository": {
"type": "git",
Expand Down
50 changes: 34 additions & 16 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,41 @@ export function injectIntoHtml(html: string, parentNodeSelector: string, toInjec
}

export function getCurrentSlideNumbers (content: string, line : number) : {hSlideNumber: number, vSlideNumber: number} | null {
const lines = content.split('\n')
if (lines && lines.length > 1) {
const linesInRange = lines.slice(0, line + 1)
let hSlideNumber = -1
let vSlideNumber = 0
linesInRange.forEach(l => {
if(l.startsWith('== ') || l.startsWith('= ')) {
hSlideNumber++
vSlideNumber = 0
}
if(l.startsWith('=== ')) {
vSlideNumber++
}
})
return {hSlideNumber, vSlideNumber}
if(!content) {
return null
}
return null

const doc = asciidoctor.load(content, {header_footer: true, sourcemap: true}) as Asciidoctor.Document
const sections = doc.getSections()
if(!sections) {
return null
}

let sectionNumber = 0
let subSectionNumber = 0
const lineInAsciidoc = line + 1

const indexOfSectionAfterCursor = sections.findIndex(s => s.getLineNumber() > lineInAsciidoc)
if(indexOfSectionAfterCursor === 0) {
return {hSlideNumber: 0, vSlideNumber: 0}
}

if(indexOfSectionAfterCursor === -1) {
sectionNumber = sections.length - 1
} else {
sectionNumber = indexOfSectionAfterCursor - 1
}

const subSections = sections[sectionNumber].getSections()
const indexOfSubSectionAfterCursor = subSections.findIndex(ss => ss.getLineNumber() > lineInAsciidoc)
if(indexOfSubSectionAfterCursor === -1) {
subSectionNumber = subSections.length
} else {
subSectionNumber = indexOfSubSectionAfterCursor
}

// add one to hSlideNumber for title slide
return {hSlideNumber: sectionNumber + 1, vSlideNumber: subSectionNumber}
}

export function showErrorMessage(message: string) {
Expand Down

0 comments on commit 36d6a40

Please sign in to comment.