Skip to content

Commit

Permalink
Small cleanup follow up on #236145
Browse files Browse the repository at this point in the history
- Don't send content as json
- Reuse existing load helper
  • Loading branch information
mjbvz committed Dec 17, 2024
1 parent 5e26b3d commit a47b13e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
21 changes: 10 additions & 11 deletions extensions/markdown-language-features/preview-src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ActiveLineMarker } from './activeLineMarker';
import { onceDocumentLoaded } from './events';
import { createPosterForVsCode } from './messaging';
import { getEditorLineNumberForPageOffset, scrollToRevealSourceLine, getLineElementForFragment } from './scroll-sync';
import { SettingsManager, getData } from './settings';
import { SettingsManager, getData, getRawData } from './settings';
import throttle = require('lodash.throttle');
import morphdom from 'morphdom';
import type { ToWebviewMessage } from '../types/previewMessaging';
Expand Down Expand Up @@ -61,8 +61,16 @@ function doAfterImagesLoaded(cb: () => void) {
}

onceDocumentLoaded(() => {
const scrollProgress = state.scrollProgress;
// Load initial html
const htmlParser = new DOMParser();
const markDownHtml = htmlParser.parseFromString(
getRawData('data-initial-md-content'),
'text/html'
);
document.body.appendChild(markDownHtml.body);

// Restore
const scrollProgress = state.scrollProgress;
addImageContexts();
if (typeof scrollProgress === 'number' && !settings.settings.fragment) {
doAfterImagesLoaded(() => {
Expand Down Expand Up @@ -353,15 +361,6 @@ document.addEventListener('click', event => {
}
}, true);

window.addEventListener('load', () => {
const htmlParser = new DOMParser();
const markDownHtml = htmlParser.parseFromString(
decodeURIComponent(getData('data-md-content')),
'text/html'
);
document.body.appendChild(markDownHtml.body);
});

window.addEventListener('scroll', throttle(() => {
updateScrollProgress();

Expand Down
8 changes: 6 additions & 2 deletions extensions/markdown-language-features/preview-src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@ export interface PreviewSettings {
readonly webviewResourceRoot: string;
}

export function getData<T = {}>(key: string): T {
export function getRawData(key: string): string {
const element = document.getElementById('vscode-markdown-preview-data');
if (element) {
const data = element.getAttribute(key);
if (data) {
return JSON.parse(data);
return data;
}
}

throw new Error(`Could not load data for ${key}`);
}

export function getData<T = {}>(key: string): T {
return JSON.parse(getRawData(key));
}

export class SettingsManager {
private _settings: PreviewSettings = getData('data-settings');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class MdDocumentRenderer {
data-settings="${escapeAttribute(JSON.stringify(initialData))}"
data-strings="${escapeAttribute(JSON.stringify(previewStrings))}"
data-state="${escapeAttribute(JSON.stringify(state || {}))}"
data-md-content="${escapeAttribute(JSON.stringify(encodeURIComponent(body.html)))}">
data-initial-md-content="${escapeAttribute(body.html)}">
<script src="${this._extensionResourcePath(resourceProvider, 'pre.js')}" nonce="${nonce}"></script>
${this._getStyles(resourceProvider, sourceUri, config, imageInfo)}
<base href="${resourceProvider.asWebviewUri(markdownDocument.uri)}">
Expand Down

0 comments on commit a47b13e

Please sign in to comment.