Skip to content

Commit

Permalink
wip on restoring links to preview pane, closes asciidoctor#435
Browse files Browse the repository at this point in the history
  • Loading branch information
danyill committed Oct 4, 2021
1 parent 23f80e6 commit b6d8482
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/asciidocParser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as vscode from 'vscode'
import * as path from 'path'
import { spawn } from 'child_process'
import { WebviewAsciidocConverter } from './webviewAsciidocConverter'

const asciidoctor = require('@asciidoctor/core')
const docbook = require('@asciidoctor/docbook-converter')
Expand Down Expand Up @@ -126,6 +127,8 @@ export class AsciidocParser {
extension_registry: registry,
}
try {
this.processor.ConverterFactory.register(new WebviewAsciidocConverter(), ['html5'])

this.document = this.processor.load(text, options)
const blocksWithLineNumber = this.document.findBy(function (b) {
return typeof b.getLineNumber() !== 'undefined'
Expand Down
2 changes: 1 addition & 1 deletion src/features/previewContentProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class AsciidocContentProvider {
data-state="${JSON.stringify(state || {}).replace(/"/g, '"')}">
<script src="${this.extensionScriptPath('pre.js')}" nonce="${nonce}"></script>
${this.getStyles(sourceUri, nonce, config, state)}
<base href="${asciidocDocument.uri.with({ scheme: 'vscode-resource' }).toString(true)}">
<base href="${editor.webview.asWebviewUri(asciidocDocument.uri).toString(true)}">
</head>
<body class="${bodyClassesVal} vscode-body ${config.scrollBeyondLastLine ? 'scrollBeyondLastLine' : ''} ${config.wordWrap ? 'wordWrap' : ''} ${config.markEditorSelection ? 'showEditorSelection' : ''}">
${body}
Expand Down
19 changes: 19 additions & 0 deletions src/webviewAsciidocConverter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const processor = require('@asciidoctor/core')()

export class WebviewAsciidocConverter {
baseConverter: any

constructor () {
this.baseConverter = processor.Html5Converter.$new()
}

convert (node, transform) {
if (node.getNodeName() === 'inline_anchor' && node.type === 'link') {
const id = node.id ? ` id="${node.id}"` : ''
const role = node.role ? ` class="${node.role}"` : ''
const title = node.title ? ` title="${node.title}"` : ''
return `<a href="${node.target}"${id}${role}${title} data-href="${node.target}">${node.text}</a>`
}
return this.baseConverter.convert(node, transform)
}
}

0 comments on commit b6d8482

Please sign in to comment.