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

Add support for markdown cells in positron notebooks #2563

Merged
merged 58 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
370a180
Add info about cell type to the PositronNotebookCell class
nstrayer Mar 21, 2024
85ccfe5
Add support for plain "error" notebook messages
nstrayer Mar 21, 2024
360e71a
Restructure notebook cells to differentiate between markup and code c…
nstrayer Mar 21, 2024
0a94da3
Distinguish between view and text model for cells better.
nstrayer Mar 21, 2024
76e7241
Add support for rendering (to html string) the content of a markdown …
nstrayer Mar 21, 2024
304a73d
Add render support for markdown.
nstrayer Mar 21, 2024
8d4e8f2
Create two distinct classes for code and markup cells and add an obse…
nstrayer Mar 22, 2024
f898401
Further separate code and markup cell models and rendering
nstrayer Mar 22, 2024
4d6bfcb
Add new button for creating markup cell next to code cell creation bu…
nstrayer Mar 22, 2024
aa47cd3
Add option for showing and hiding the editor for a markup cell
nstrayer Mar 22, 2024
0d0c189
Fix overflowing action-bar border when in collapsed markup cell mode
nstrayer Mar 22, 2024
138a2a3
Add message about empty markup cell so user's not super confused
nstrayer Mar 22, 2024
d7c4ec3
Add logic to hide the cell outlines in markdown cells if the editor i…
nstrayer Mar 22, 2024
4b041a2
Switch off of notebook skeleton for the markup cell, too
nstrayer Mar 25, 2024
84c9349
markdown now renders in webview so we can successfully open images.
nstrayer Mar 26, 2024
cbb7546
Add communication from webview to outside with info about height of c…
nstrayer Mar 26, 2024
461dff9
mend
nstrayer Mar 26, 2024
80a56a4
Make action bar look a bit better when overlapping content in collaps…
nstrayer Mar 26, 2024
97ccbe2
mend
nstrayer Mar 26, 2024
cf62989
Add new skeleton for extension used for converting images to data urls
nstrayer Mar 27, 2024
e0c17f4
Add ability to specify custom components for a given tag name when re…
nstrayer Mar 27, 2024
71d3005
Render images by converting to data-url using positron notebook helpe…
nstrayer Mar 27, 2024
936032a
Add placeholder image for when images are not yet loaded
nstrayer Mar 27, 2024
80abc51
Switch to using markdown-to-html rendering from the `markdown.api.ren…
nstrayer Mar 28, 2024
a228322
Centralize the markdown rendering logic into a single component and a…
nstrayer Mar 28, 2024
0b72230
Add basic styles to markdown rendered content taken from existing not…
nstrayer Mar 28, 2024
d67f171
Remove unused export from extension left-over from copy-pasting
nstrayer Mar 28, 2024
65245f2
Remove unneccesary space in notebook cell component
nstrayer Mar 28, 2024
bc69b23
Switch to using markdown instead of markup for... ahem, markdown cells.
nstrayer Mar 28, 2024
7f40588
Switch to simpler name for general purpose notebook cell.
nstrayer Mar 28, 2024
bc2640e
Move type-guard functions into main positron notebook cell class
nstrayer Mar 28, 2024
43b99e1
Make "running" a cell for markdown cells the equivalent of toggling t…
nstrayer Mar 28, 2024
322c932
Update cell running method to "run" markdown cells as well as code cells
nstrayer Mar 28, 2024
5a5ea7f
Switch to async version of readfile.
nstrayer Mar 28, 2024
d4af14e
Debounce error messages and also fix bug where errored readFile calls…
nstrayer Mar 28, 2024
6227d04
Add ability to open links using opener service
nstrayer Mar 28, 2024
dd3f53f
Consolidate html link clicking logic into same componentOverrides fie…
nstrayer Mar 28, 2024
35e8e80
Update extensions/positron-notebooks/extension.webpack.config.js
nstrayer Mar 29, 2024
38654e2
Update extensions/positron-notebooks/src/extension.ts
nstrayer Mar 29, 2024
b7cc4eb
Declare command in contributes section.
nstrayer Mar 29, 2024
d5211f6
Be better about mime-type mapping
nstrayer Mar 29, 2024
8f82d6e
Pass responsibilities for handling bad conversions to the front-end b…
nstrayer Mar 29, 2024
cb92717
Localize everything!
nstrayer Mar 29, 2024
d9d176d
Make error handing handle entire markdown rendering command pipeline …
nstrayer Mar 29, 2024
f4b4bc9
Remove left-over comments in tsconfig from original copy location
nstrayer Mar 29, 2024
9b34e68
Add helper function for the task of running a command with a timeout …
nstrayer Mar 29, 2024
0d5a5e3
Restructure timeout command function to not allow callbacks to be cal…
nstrayer Mar 29, 2024
b7402a8
Update timeout command to use existing cancel token class and to go b…
nstrayer Apr 1, 2024
ff2afc6
Dont attempt to convert remote images
nstrayer Apr 1, 2024
a51d379
Only show most recent error message for conversion and clear stale me…
nstrayer Apr 1, 2024
1ea9e77
Don't pass source to error state to avoid failed `GET` request
nstrayer Apr 1, 2024
fbf825a
Move `renderHtml()` into positron subfolder
nstrayer Apr 1, 2024
ea726b7
Fix name of script containing promiseWithTimeout function
nstrayer Apr 1, 2024
be302cb
Add slightly better error message in timeout case
nstrayer Apr 1, 2024
af266b0
Add styles for rendering and error messages in markdown component
nstrayer Apr 1, 2024
a59f4a7
Fix utterly unneccesary string concatenation.
nstrayer Apr 1, 2024
b829e7f
Fix case-sensitivity of mime-type checking.
nstrayer Apr 1, 2024
1182b4f
Replace custom promiseWithTimeout() with existing functions
nstrayer Apr 1, 2024
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 build/gulpfile.extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const compilations = [
'positron-connections/tsconfig.json',
'positron-javascript/tsconfig.json',
'positron-notebook-controllers/tsconfig.json',
'positron-notebooks/tsconfig.json',
'positron-r/tsconfig.json',
'positron-rstudio-keymap/tsconfig.json',
'positron-python/tsconfig.json',
Expand Down
1 change: 1 addition & 0 deletions build/npm/dirs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const dirs = [
'extensions/positron-connections',
'extensions/positron-javascript',
'extensions/positron-notebook-controllers',
'extensions/positron-notebooks',
'extensions/positron-r',
'extensions/positron-rstudio-keymap',
'extensions/positron-python',
Expand Down
20 changes: 20 additions & 0 deletions extensions/positron-notebooks/extension.webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*---------------------------------------------------------------------------------------------
* Copyright (C) 2024 Posit Software, PBC. All rights reserved.
*--------------------------------------------------------------------------------------------*/

//@ts-check

'use strict';

const withDefaults = require('../shared.webpack.config');

module.exports = withDefaults({
context: __dirname,
entry: {
extension: './src/extension.ts',
},
externals: {
'vscode': { commonjs: 'vscode' },
'express': { commonjs: 'express' }
}
});
50 changes: 50 additions & 0 deletions extensions/positron-notebooks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
nstrayer marked this conversation as resolved.
Show resolved Hide resolved
"name": "positron-notebooks-helpers",
"displayName": "Positron Notebooks Helpers",
"description": "Positron Notebook Helpers",
"version": "1.0.0",
"publisher": "vscode",
"engines": {
"vscode": "^1.65.0"
},
"categories": [
"Other"
],
"activationEvents": [
"onStartupFinished"
],
"contributes": {
"commands": [
{
"command": "positronNotebookHelpers.convertImageToBase64",
"title": "Convert Image to Base64"
}
]
},
"main": "./out/extension.js",
"scripts": {
"vscode:prepublish": "yarn run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"lint": "eslint src --ext ts"
},
"dependencies": {},
"devDependencies": {
"@types/glob": "^7.2.0",
"@types/mocha": "^9.1.0",
"@types/node": "14.x",
"@typescript-eslint/eslint-plugin": "^5.12.1",
"@typescript-eslint/parser": "^5.12.1",
"@vscode/test-electron": "^2.1.2",
"eslint": "^8.9.0",
"glob": "^7.2.0",
"mocha": "^9.2.1",
"ts-node": "^10.9.1",
"typescript": "^4.5.5",
"vsce": "^2.11.0"
},
"repository": {
"type": "git",
"url": "https://github.com/posit-dev/positron"
}
}
82 changes: 82 additions & 0 deletions extensions/positron-notebooks/src/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*---------------------------------------------------------------------------------------------
* Copyright (C) 2024 Posit Software, PBC. All rights reserved.
*--------------------------------------------------------------------------------------------*/

import path from 'path';
import * as vscode from 'vscode';
import { readFile } from 'fs';

// Make sure this matches the error message type defined where used
// (src/vs/workbench/contrib/positronNotebook/browser/notebookCells/DeferredImage.tsx)
type CoversionErrorMsg = {
status: 'error';
message: string;
};

/**
* Activates the extension.
* @param context An ExtensionContext that contains the extention context.
*/
export function activate(context: vscode.ExtensionContext) {
// Command that converts an image from the local file-system to a base64 string.
context.subscriptions.push(
vscode.commands.registerCommand(
'positronNotebookHelpers.convertImageToBase64',
async (imageSrc: string, baseLoc: string) => new Promise<string | CoversionErrorMsg>((resolve) => {
const fullImagePath = path.join(baseLoc, imageSrc);
const fileExtension = path.extname(imageSrc).slice(1);
nstrayer marked this conversation as resolved.
Show resolved Hide resolved
const mimeType = mimeTypeMap[fileExtension.toLowerCase()];
nstrayer marked this conversation as resolved.
Show resolved Hide resolved
if (!mimeType) {
resolve({
status: 'error',
message: `Unsupported file type: "${fileExtension}."`,
});
return;
}
try {
readFile(fullImagePath, (err, data) => {
if (err) {
resolve({
status: 'error',
message: err.message,
});
} else if (!data) {
resolve({
status: 'error',
message: `No data found in file "${fullImagePath}."`,
});
} else {
resolve(`data:${mimeType};base64,${data.toString('base64')}`);
}
});
} catch (e) {
return {
type: 'error',
message: e instanceof Error ? e.message : `Error occured while converting image ${fullImagePath} to base64.`,
};
}
})
)
);
}


/**
* Map image file extension to MIME type.
*
* Supports all the 'image' types from [this list](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types)
*/
const mimeTypeMap: Record<string, string> = {
png: 'image/png',
apng: 'image/apng',
avif: 'image/avif',
ico: 'image/vnd.microsoft.icon',
jpeg: 'image/jpeg',
jpg: 'image/jpeg',
gif: 'image/gif',
bmp: 'image/bmp',
webp: 'image/webp',
svg: 'image/svg+xml',
tiff: 'image/tiff',
tif: 'image/tiff',
};
17 changes: 17 additions & 0 deletions extensions/positron-notebooks/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "ES2020",
"outDir": "out",
"esModuleInterop": true,
"lib": ["ES2020"],
"sourceMap": true,
"rootDir": "src",
"strict": true,
"typeRoots": ["./node_modules/@types"],
"paths": {
"*": ["./node_modules/*"]
}
},
"include": ["src/**/*", "../../src/vscode-dts/vscode.d.ts"]
}
Loading
Loading