Skip to content

Commit

Permalink
resolves #805 fix web preview (#806)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie authored Oct 24, 2023
1 parent d036093 commit 862651d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 44 deletions.
2 changes: 1 addition & 1 deletion extension-browser.webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = {
'https': false,
'url': false,
'zlib': false,
'os': false,
'os': require.resolve('os-browserify/browser'),
'child_process': false,
'crypto': false,
'stream': false,
Expand Down
66 changes: 66 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@
"@fontsource/open-sans": "4.5.14",
"@fontsource/roboto-mono": "4.5.10",
"@highlightjs/cdn-assets": "~11.8.0",
"@playwright/test": "^1.39.0",
"@types/lodash.throttle": "~4.1",
"@types/mocha": "~9.1",
"@types/node": "12.20.55",
Expand Down Expand Up @@ -658,6 +659,7 @@
"asciidoctor-kroki": "0.18.1",
"html-entities": "^2.4.0",
"js-yaml": "^4.1.0",
"os-browserify": "^0.3.0",
"querystring": "^0.2.1",
"tty-browserify": "^0.0.1",
"util": "^0.12.5",
Expand Down
58 changes: 15 additions & 43 deletions src/features/asciidoctorConfig.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import * as path from 'path'
import * as vscode from 'vscode'
import { Asciidoctor } from '@asciidoctor/core'
import { AsciidoctorProcessor } from '../asciidoctorProcessor'
import { getWorkspaceFolder } from '../util/workspace'

const MAX_DEPTH_SEARCH_ASCIIDOCCONFIG = 100

export interface AsciidoctorConfigProvider {
activate(registry: Asciidoctor.Extensions.Registry, documentUri: vscode.Uri): Promise<void>;
Expand Down Expand Up @@ -46,49 +42,25 @@ export class AsciidoctorConfig implements AsciidoctorConfigProvider {
}
}

async function exists (uri: vscode.Uri) {
try {
await vscode.workspace.fs.stat(uri)
return true
} catch (err) {
if (err && err.code === 'FileNotFound') {
return false
}
throw err
}
}

export async function getAsciidoctorConfigContent (documentUri: vscode.Uri): Promise<String | undefined> {
const workspaceFolder = getWorkspaceFolder(documentUri)
if (workspaceFolder === undefined) {
const asciidoctorConfigs = (await vscode.workspace.findFiles('**/.{asciidoctorconfig.adoc,asciidoctorconfig}', null))
.filter((uri) => {
const documentParentDirectory = documentUri.path.slice(0, documentUri.path.lastIndexOf('/'))
const asciidoctorConfigParentDirectory = uri.path.slice(0, uri.path.lastIndexOf('/'))
return documentParentDirectory.startsWith(asciidoctorConfigParentDirectory)
})
.sort((a, b) => a.path.localeCompare(b.path))
if (asciidoctorConfigs.length === 0) {
return undefined
}

const configContents: string[] = []
let currentFile: string = documentUri.fsPath
let increment = 0
while (currentFile !== undefined && currentFile !== workspaceFolder.uri.fsPath && increment < MAX_DEPTH_SEARCH_ASCIIDOCCONFIG) {
increment++
currentFile = path.dirname(currentFile)
configContents.push(await getConfigContent(currentFile, '.asciidoctorconfig.adoc'))
configContents.push(await getConfigContent(currentFile, '.asciidoctorconfig'))
}

const configContentsOrderedAndFiltered = configContents
.filter((config) => config !== undefined)
.reverse()

if (configContentsOrderedAndFiltered.length > 0) {
return configContentsOrderedAndFiltered.join('\n\n')
const configContents = []
for (const asciidoctorConfig of asciidoctorConfigs) {
const asciidoctorConfigContent = new TextDecoder().decode(await vscode.workspace.fs.readFile(asciidoctorConfig))
const asciidoctorConfigParentDirectory = asciidoctorConfig.path.slice(0, asciidoctorConfig.path.lastIndexOf('/'))
configContents.push(`:asciidoctorconfigdir: ${asciidoctorConfigParentDirectory}\n\n${asciidoctorConfigContent.trim()}\n\n`)
}
return undefined
}

async function getConfigContent (folderPath: string, configFilename: string) {
const asciidoctorConfigUri = vscode.Uri.joinPath(vscode.Uri.file(folderPath), configFilename)
if (await exists(asciidoctorConfigUri)) {
const asciidoctorConfigContent = new TextDecoder().decode(await vscode.workspace.fs.readFile(asciidoctorConfigUri))
return `:asciidoctorconfigdir: ${folderPath}\n\n${asciidoctorConfigContent.trim()}\n\n`
if (configContents.length > 0) {
return configContents.join('\n\n')
}
return undefined
}

0 comments on commit 862651d

Please sign in to comment.