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

Basic syntax highlight support for code blocks #526

Merged
merged 74 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
c81b847
Set up highlight.js
eliandoran Oct 26, 2024
bf28005
Create dedicated file for syntax highlight
eliandoran Oct 26, 2024
579b3f4
Carry over highlighter initialization
eliandoran Oct 26, 2024
6078620
Carry over code block highlighting
eliandoran Oct 26, 2024
f80cf0a
Add limit to blocks highlighting
eliandoran Oct 26, 2024
c7e1362
Merge branch 'develop' into feature/syntax_highlight
eliandoran Oct 26, 2024
6c4513f
client: Enable syntax highlight for read-only notes
eliandoran Oct 27, 2024
1e9324c
client: Support custom language types for highlight
eliandoran Oct 27, 2024
e931df7
client: Fix duplication when requesting scripts
eliandoran Oct 27, 2024
c4bd4eb
client: Respect user language selection for editor
eliandoran Oct 27, 2024
3a609d5
client: Fix highlighting for JavaScript
eliandoran Oct 27, 2024
a393237
client: Add Javadoc for newly introduced methods
eliandoran Oct 27, 2024
7505db2
client: Implement auto syntax highlighting
eliandoran Oct 27, 2024
9e3b915
client: Use translation for auto-detect
eliandoran Oct 27, 2024
1fb0b74
client: Use same mechanism for read-only notes
eliandoran Oct 27, 2024
7354fb5
client,server: List syntax highlighting themes
eliandoran Oct 27, 2024
60b6f7d
client: Allow switching theme
eliandoran Oct 27, 2024
7952a5a
client: Fix order of options
eliandoran Oct 27, 2024
e1ae014
server: Remove dashes from syntax theme name
eliandoran Oct 27, 2024
fe4dbae
client: Apply highlighting theme on refresh
eliandoran Oct 27, 2024
1a25f60
client: Fix background color
eliandoran Oct 27, 2024
87bc142
client: Fix foreground color
eliandoran Oct 27, 2024
d2235a1
client: Improve style for code blocks
eliandoran Oct 27, 2024
f947a03
client: Apply background to read-only code blocks as well
eliandoran Oct 27, 2024
0d270cb
client: Use 3px shadow for dark theme
eliandoran Oct 27, 2024
2fdff29
client: Apply syntax highlight in real-time
eliandoran Oct 27, 2024
83f5b47
client: Set up simple preview for syntax highlight
eliandoran Oct 27, 2024
e843f1a
client: Fix background of preview
eliandoran Oct 27, 2024
514653f
client: Fix flicker of preview
eliandoran Oct 27, 2024
41e3163
client: Fix flicker of font selection
eliandoran Oct 27, 2024
6717b1b
client: Rephrase section
eliandoran Oct 27, 2024
ef5f2c6
client: Rephrase theme section
eliandoran Oct 27, 2024
e68d070
client: Set up localization for syntax highlighting section
eliandoran Oct 27, 2024
96b9042
client: Rename option to codeBlockTheme
eliandoran Oct 27, 2024
e65d4cd
client: Rename endpoint to codeblock-themes
eliandoran Oct 27, 2024
52bc28d
client: Rename section to CodeBlockOptions
eliandoran Oct 27, 2024
b8569ea
client, server: Create option to control word wrapping for code blocks
eliandoran Oct 27, 2024
5109c07
client: Toggle word wrapping for code blocks
eliandoran Oct 27, 2024
5682b2d
client: Translate word wrapping
eliandoran Oct 27, 2024
b8eb09b
server: Refactor code block theme search into own service
eliandoran Oct 27, 2024
dbb5e0e
server: Add friendlier names for color themes
eliandoran Oct 27, 2024
9c55203
client: Add credits
eliandoran Oct 27, 2024
48f9f07
Format theme names
adoriandoran Oct 28, 2024
bda8173
Improve the sample code
adoriandoran Oct 28, 2024
36fde2b
Tweak the language badge of code blocks
adoriandoran Oct 28, 2024
70cee7d
Tweak the shadow of code blocks
adoriandoran Oct 28, 2024
d3d001d
Tweak (again) the shadow of code blocks
adoriandoran Oct 28, 2024
2f4ed92
Prevent the language badge to be scrolled in code boxes
adoriandoran Oct 28, 2024
b9ebc66
Customize the scrollbar in code boxes for WebKit-based browsers
adoriandoran Oct 28, 2024
b112cb6
Tweak the padding of the language badges
adoriandoran Oct 28, 2024
60426ea
Fix word-wrapping
adoriandoran Oct 29, 2024
2459bbf
Improve the layout of the "Word wrapping" checkbox
adoriandoran Oct 29, 2024
f13d88c
Add a background color transition for the code sample
adoriandoran Oct 29, 2024
bbc038f
Merge remote-tracking branch 'origin/develop' into feature/syntax_hig…
eliandoran Oct 31, 2024
ac13291
client,server: Allow disabling syntax highlight
eliandoran Oct 31, 2024
90dffdc
client: Enable preview for word wrap
eliandoran Oct 31, 2024
ae60f8c
client: Group color themes by dark/light
eliandoran Oct 31, 2024
cc0b3db
client: Translate dark/light color theme groups
eliandoran Oct 31, 2024
a690155
client: Improve group for no theme
eliandoran Oct 31, 2024
f57ab4b
client: Fix word wrap preview being in reverse
eliandoran Oct 31, 2024
c3e10b2
client: Remove syntax highlight in preview when disabled
eliandoran Oct 31, 2024
4d783f1
client: Fix color theme leak when deactivating highlighting
eliandoran Oct 31, 2024
3af29a7
client: Refactor syntax highlighting for read-only text into service
eliandoran Oct 31, 2024
dfa4f3c
client: Apply syntax highlight to note preview
eliandoran Oct 31, 2024
00209ec
client: Apply syntax highlight to included notes
eliandoran Oct 31, 2024
9e3c1b4
client: Don't load syntax highlighter when not needed
eliandoran Oct 31, 2024
0a6815e
Merge remote-tracking branch 'origin/develop' into feature/syntax_hig…
eliandoran Nov 1, 2024
df3f51d
electron: Fix loading of highlight.js
eliandoran Nov 1, 2024
e508313
electron: Fix deprecation warning
eliandoran Nov 1, 2024
5c66e3f
server: Initialize code block theme for old databases as well
eliandoran Nov 1, 2024
92c588d
server: Implement color theme migration based on existing theme
eliandoran Nov 1, 2024
4e94558
server: Add some documentation
eliandoran Nov 1, 2024
1554e25
server: Add documentation for code_block_theme
eliandoran Nov 1, 2024
b14065d
server: Address self-review
eliandoran Nov 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
5 changes: 4 additions & 1 deletion bin/copy-dist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const copy = async () => {
"node_modules/katex/dist/katex.min.js",
"node_modules/katex/dist/contrib/mhchem.min.js",
"node_modules/katex/dist/contrib/auto-render.min.js",
"node_modules/@highlightjs/cdn-assets/highlight.min.js"
];

for (const file of nodeModulesFile) {
Expand Down Expand Up @@ -89,7 +90,9 @@ const copy = async () => {
"node_modules/codemirror/addon/",
"node_modules/codemirror/mode/",
"node_modules/codemirror/keymap/",
"node_modules/mind-elixir/dist/"
"node_modules/mind-elixir/dist/",
"node_modules/@highlightjs/cdn-assets/languages",
"node_modules/@highlightjs/cdn-assets/styles"
];

for (const folder of nodeModulesFolder) {
Expand Down
11 changes: 7 additions & 4 deletions libraries/ckeditor/ckeditor-content.css
Original file line number Diff line number Diff line change
Expand Up @@ -526,16 +526,19 @@
/* @ckeditor/ckeditor5-code-block/theme/codeblock.css */
.ck-content pre {
padding: 1em;
color: hsl(0, 0%, 20.8%);
background: hsla(0, 0%, 78%, 0.3);
border: 1px solid hsl(0, 0%, 77%);
border-radius: 2px;
text-align: left;
direction: ltr;
tab-size: 4;
white-space: pre-wrap;
font-style: normal;
min-width: 200px;
border: 0px;
border-radius: 6px;
box-shadow: 1px 1px 6px rgba(0, 0, 0, 0.2);
}
.ck-content pre:not(.hljs) {
color: hsl(0, 0%, 20.8%);
background: hsla(0, 0%, 78%, 0.3);
}
/* @ckeditor/ckeditor5-code-block/theme/codeblock.css */
.ck-content pre code {
Expand Down
2 changes: 1 addition & 1 deletion libraries/ckeditor/ckeditor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion libraries/ckeditor/ckeditor.js.map

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@braintree/sanitize-url": "7.1.0",
"@electron/remote": "2.1.2",
"@excalidraw/excalidraw": "0.17.6",
"@highlightjs/cdn-assets": "11.10.0",
"archiver": "7.0.1",
"async-mutex": "0.5.0",
"autocomplete.js": "0.38.1",
Expand Down
9 changes: 9 additions & 0 deletions src/becca/entities/rows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,18 @@ export interface RecentNoteRow {
utcDateCreated?: string;
}

/**
* Database representation of an option.
*
* Options are key-value pairs that are used to store information such as user preferences (for example
* the current theme, sync server information), but also information about the state of the application).
*/
export interface OptionRow {
/** The name of the option. */
name: string;
/** The value of the option. */
value: string;
/** `true` if the value should be synced across multiple instances (e.g. locale) or `false` if it should be local-only (e.g. theme). */
isSynced: boolean;
utcDateModified?: string;
}
Expand Down
3 changes: 3 additions & 0 deletions src/public/app/services/content_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import treeService from "./tree.js";
import FNote from "../entities/fnote.js";
import FAttachment from "../entities/fattachment.js";
import imageContextMenuService from "../menus/image_context_menu.js";
import { applySyntaxHighlight } from "./syntax_highlight.js";

let idCounter = 1;

Expand Down Expand Up @@ -105,6 +106,8 @@ async function renderText(note, $renderedContent) {
for (const el of referenceLinks) {
await linkService.loadReferenceLinkTitle($(el));
}

await applySyntaxHighlight($renderedContent);
} else {
await renderChildrenList($renderedContent, note);
}
Expand Down
66 changes: 64 additions & 2 deletions src/public/app/services/library_loader.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import mimeTypesService from "./mime_types.js";
import optionsService from "./options.js";

const CKEDITOR = {"js": ["libraries/ckeditor/ckeditor.js"]};

const CODE_MIRROR = {
Expand Down Expand Up @@ -84,18 +87,44 @@ const MIND_ELIXIR = {
]
};

const HIGHLIGHT_JS = {
js: () => {
const mimeTypes = mimeTypesService.getMimeTypes();
const scriptsToLoad = new Set();
scriptsToLoad.add("node_modules/@highlightjs/cdn-assets/highlight.min.js");
for (const mimeType of mimeTypes) {
if (mimeType.enabled && mimeType.highlightJs) {
scriptsToLoad.add(`node_modules/@highlightjs/cdn-assets/languages/${mimeType.highlightJs}.min.js`);
}
}

const currentTheme = optionsService.get("codeBlockTheme");
loadHighlightingTheme(currentTheme);

return Array.from(scriptsToLoad);
}
};

async function requireLibrary(library) {
if (library.css) {
library.css.map(cssUrl => requireCss(cssUrl));
}

if (library.js) {
for (const scriptUrl of library.js) {
for (const scriptUrl of unwrapValue(library.js)) {
await requireScript(scriptUrl);
}
}
}

function unwrapValue(value) {
if (typeof value === "function") {
return value();
}

return value;
}

// we save the promises in case of the same script being required concurrently multiple times
const loadedScriptPromises = {};

Expand Down Expand Up @@ -127,9 +156,41 @@ async function requireCss(url, prependAssetPath = true) {
}
}

let highlightingThemeEl = null;
function loadHighlightingTheme(theme) {
if (!theme) {
return;
}

if (theme === "none") {
// Deactivate the theme.
if (highlightingThemeEl) {
highlightingThemeEl.remove();
highlightingThemeEl = null;
}
return;
}

if (!highlightingThemeEl) {
highlightingThemeEl = $(`<link rel="stylesheet" type="text/css" />`);
$("head").append(highlightingThemeEl);
}

let url;
const defaultPrefix = "default:";
if (theme.startsWith(defaultPrefix)) {
url = `${window.glob.assetPath}/node_modules/@highlightjs/cdn-assets/styles/${theme.substr(defaultPrefix.length)}.min.css`;
}

if (url) {
highlightingThemeEl.attr("href", url);
}
}

export default {
requireCss,
requireLibrary,
loadHighlightingTheme,
CKEDITOR,
CODE_MIRROR,
ESLINT,
Expand All @@ -143,5 +204,6 @@ export default {
EXCALIDRAW,
MARKJS,
I18NEXT,
MIND_ELIXIR
MIND_ELIXIR,
HIGHLIGHT_JS
}
Loading