Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh markdown preview on updating configuration #20

Merged
merged 3 commits into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixed

- Refresh Markdown preview on updating configuration (for VSCode >= 1.34) ([#20](https://github.com/marp-team/marp-vscode/pull/20))
- Use Marp Core options when rendering by Marp ([#21](https://github.com/marp-team/marp-vscode/pull/21))

## v0.1.0 - 2019-03-22
Expand Down
1 change: 1 addition & 0 deletions src/__mocks__/vscode.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const workspace = {
getConfiguration: jest.fn(() => new Map()),
onDidChangeConfiguration: jest.fn(),
}
14 changes: 12 additions & 2 deletions src/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,18 @@ beforeEach(() => mockWorkspaceConfig())
afterEach(() => jest.restoreAllMocks())

describe('#activate', () => {
it('contains #extendMarkdownIt', () =>
expect(activate()).toEqual(expect.objectContaining({ extendMarkdownIt })))
const extContext: any = { subscriptions: { push: jest.fn() } }

it('contains #extendMarkdownIt', () => {
expect(activate(extContext)).toEqual(
expect.objectContaining({ extendMarkdownIt })
)
})

it('starts tracking to change of configurations', () => {
const onDidChgConf = workspace.onDidChangeConfiguration as jest.Mock
expect(onDidChgConf).toBeCalledWith(expect.any(Function))
})
})

describe('#extendMarkdownIt', () => {
Expand Down
15 changes: 13 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Marp } from '@marp-team/marp-core'
import { workspace } from 'vscode'
import { ExtensionContext, commands, workspace } from 'vscode'

const frontMatterRegex = /^-{3,}\s*([^]*?)^\s*-{3}/m
const marpDirectiveRegex = /^marp\s*:\s*true\s*$/m
const marpConfiguration = () => workspace.getConfiguration('markdown.marp')
const marpVscode = Symbol('marp-vscode')
const shouldRefreshConfs = ['markdown.marp.enableHtml', 'window.zoomLevel']

const detectMarpFromFrontMatter = (markdown: string): boolean => {
const m = markdown.match(frontMatterRegex)
Expand Down Expand Up @@ -51,4 +52,14 @@ export function extendMarkdownIt(md: any) {
return md
}

export const activate = () => ({ extendMarkdownIt })
export const activate = ({ subscriptions }: ExtensionContext) => {
subscriptions.push(
workspace.onDidChangeConfiguration(e => {
if (shouldRefreshConfs.some(c => e.affectsConfiguration(c))) {
commands.executeCommand('markdown.preview.refresh')
}
})
)

return { extendMarkdownIt }
}