diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d880cd..4847e7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/package.json b/package.json index dd317cc..412fc81 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/utils.ts b/src/utils.ts index 71023e3..efa4c5c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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) {