Ready for PR

This commit is contained in:
Mayuran Visakan 2023-06-04 12:25:18 +01:00
parent 08077be6c0
commit e7bf2bbed7
7 changed files with 105 additions and 101 deletions

57
main.js

File diff suppressed because one or more lines are too long

View file

@ -105,19 +105,15 @@ export function codeblockHighlight(settings: CodeblockCustomizerSettings) {
}// deduplicateCodeblocks
buildDecorations(view: EditorView): DecorationSet {
let lineNumber = 1;
let lineNumber = 0;
let HL = [];
let altHL = [];
let lineNumberOffset = 0;
const Exclude = this.settings.ExcludeLangs;
const ExcludeLangs = splitAndTrimString(Exclude);
let showNumbers = true;
// const Exclude = this.settings.ExcludeLangs;
// const ExcludeLangs = splitAndTrimString(Exclude);
let bExclude = false;
const alternateColors = this.settings.alternateColors || [];
const bDisplayCodeBlockLanguage = this.settings.bDisplayCodeBlockLanguage;
const bDisplayCodeBlockIcon = this.settings.bDisplayCodeBlockIcon;
const bAlwaysDisplayCodeblockLang = this.settings.header.bAlwaysDisplayCodeblockLang;
const bAlwaysDisplayCodeblockIcon = this.settings.header.bAlwaysDisplayCodeblockIcon;
const linenumbers = this.settings.bEnableLineNumbers;
const decorations = [];
if (!view.visibleRanges || view.visibleRanges.length === 0) {
@ -150,7 +146,8 @@ export function codeblockHighlight(settings: CodeblockCustomizerSettings) {
}
if (startLine) {
lineNumber = 0;
lineNumberOffset = 0; //TODO (@mayurankv) Set offset here
lineNumberOffset = 0; //searchString(codeBlockFirstLine, "ln:")//TODO (@mayurankv) Set line number offset here - Should be ln_value - 1 since it is offset, not starting line number - 0 if true OR false
showNumbers = true; //TODO (@mayurankv) Set showNumbers to be true if ln:<number> or ln:true or false if ln:false
const params = searchString(lineText, "HL:");
HL = getHighlightedLines(params);
altHL = [];
@ -173,7 +170,7 @@ export function codeblockHighlight(settings: CodeblockCustomizerSettings) {
decorations.push(Decoration.line({attributes: {class: lineClass}}).range(node.from));
decorations.push(Decoration.line({}).range(node.from));
decorations.push(Decoration.widget({ widget: new LineNumberWidget((startLine || endLine) ? " " : lineNumber+lineNumberOffset),}).range(node.from));
decorations.push(Decoration.widget({ widget: new LineNumberWidget((startLine || endLine) ? " " : lineNumber+lineNumberOffset,showNumbers),}).range(node.from));
lineNumber++;
}
},
@ -205,18 +202,17 @@ function compareArrays(array1, array2) {
}// compareArrays
class LineNumberWidget extends WidgetType {
constructor(private lineNumber: number) {
constructor(private lineNumber: number, private showNumbers: boolean) {
super();
}
eq(other: LineNumberWidget) {
return this.lineNumber === other.lineNumber;
return this.lineNumber === other.lineNumber && this.showNumbers === other.showNumbers;
}
toDOM(view: EditorView): HTMLElement {
const container = document.createElement("span");
//TODO (@mayurankv) If the numbers are set with ln parameter, then make this an if statement and instead add a new class called 'codeblock-customizer-line-number-specific' which can then be targeted by css
container.classList.add("codeblock-customizer-line-number");
container.classList.add(`codeblock-customizer-line-number${this.showNumbers?'':'-hide'}`);
container.innerText = `${this.lineNumber}`;
return container;

View file

@ -64,6 +64,8 @@ export async function ReadingView(codeBlockElement: HTMLElement, context: Markdo
const linesToHighlight = getHighlightedLines(highlightedLinesParams);
const FileName = searchString(codeBlockFirstLine, "file:");
const Fold = searchString(codeBlockFirstLine, "fold");
const lineNumberOffset = 0; //searchString(codeBlockFirstLine, "ln:")//TODO (@mayurankv) Set line number offset here - Should be ln_value - 1 since it is offset, not starting line number - 0 if true OR false
const showNumbers = true; //TODO (@mayurankv) Set showNumbers to be true if ln:<number> or ln:true or false if ln:false
const alternateColors = plugin.settings.alternateColors || [];
let altHL = [];
for (const { name } of alternateColors) {
@ -83,7 +85,7 @@ export async function ReadingView(codeBlockElement: HTMLElement, context: Markdo
codeBlockPreElement.classList.add(`codeblock-customizer-pre`);
}
AddHeaderAndHighlight(isCodeBlockExcluded, FileName, codeBlockPreElement, codeBlockLang, Fold, codeElements, linesToHighlight, altHL );
AddHeaderAndHighlight(isCodeBlockExcluded, FileName, codeBlockPreElement, codeBlockLang, Fold, codeElements, showNumbers, lineNumberOffset, linesToHighlight, altHL );
}// ReadingView
function isAdmonition(lineText: string): boolean {
@ -118,10 +120,9 @@ function HeaderWidget(preElements, textToDisplay: string, specificHeader: boolea
}// HeaderWidget
function createLineNumberElement(lineNumber) {
function createLineNumberElement(lineNumber,showNumbers) {
const lineNumberWrapper = document.createElement("div");
//TODO (@mayurankv) If the numbers are set with ln parameter, then make this an if statement and instead add a new class called 'codeblock-customizer-line-number-specific' which can then be targeted by css
lineNumberWrapper.classList.add(`codeblock-customizer-line-number`);
lineNumberWrapper.classList.add(`codeblock-customizer-line-number${showNumbers?'':'-hide'}`);
lineNumberWrapper.setText(lineNumber);
return lineNumberWrapper;
@ -135,16 +136,13 @@ function createLineTextElement(line) {
return lineContentWrapper;
}// createLineTextElement
function highlightLines(codeElements, linesToHighlight, altHL) {
function highlightLines(codeElements, showNumbers, lineNumberOffset, linesToHighlight, altHL) {
for (let i = 0; i < codeElements.length; i++) {
const lines = codeElements[i].innerHTML.split("\n");
const preElm = codeElements[i].parentNode;
if (preElm === null || preElm.nodeName !== "PRE") { // only process pre > code elements
if (preElm === null || preElm.nodeName !== "PRE") // only process pre > code elements
return;
}
const lineNumberOffset = 0; //TODO (@mayurankv) Set offset here
codeElements[i].innerHTML = "";
for (let j = 0; j < lines.length - 1; j++) {
const line = lines[j];
@ -163,7 +161,7 @@ function highlightLines(codeElements, linesToHighlight, altHL) {
codeElements[i].appendChild(lineWrapper);
// create line number element
const lineNumberEl = createLineNumberElement(lineNumber+lineNumberOffset);
const lineNumberEl = createLineNumberElement(lineNumber+lineNumberOffset,showNumbers);
lineWrapper.appendChild(lineNumberEl);
// create line text element
@ -173,7 +171,7 @@ function highlightLines(codeElements, linesToHighlight, altHL) {
}
}// highlightLines
function AddHeaderAndHighlight(isCodeBlockExcluded, fileName, codeBlockPreElement, codeBlockLang, Fold, codeElements, linesToHighlight, altHL ){
function AddHeaderAndHighlight(isCodeBlockExcluded, fileName, codeBlockPreElement, codeBlockLang, Fold, codeElements, showNumbers, lineNumberOffset, linesToHighlight, altHL ){
if (!isCodeBlockExcluded) {
let specificHeader = true;
if (fileName === null || fileName === "") {
@ -185,8 +183,8 @@ function AddHeaderAndHighlight(isCodeBlockExcluded, fileName, codeBlockPreElemen
}
}
HeaderWidget(codeBlockPreElement, fileName, specificHeader, getLanguageName(codeBlockLang), Fold);
highlightLines(codeElements, linesToHighlight, altHL);
highlightLines(codeElements, showNumbers, lineNumberOffset, linesToHighlight, altHL);
if (codeBlockPreElement.parentElement) {
codeBlockPreElement.parentElement.classList.add(`codeblock-customizer-pre-parent`);
}
@ -195,15 +193,16 @@ function AddHeaderAndHighlight(isCodeBlockExcluded, fileName, codeBlockPreElemen
function PDFExport(codeBlockElement: HTMLElement, plugin: CodeblockCustomizerPlugin, codeBlockFirstLines: string[]) {
const codeBlocks = codeBlockElement.querySelectorAll('pre > code');
const pluginSettings = plugin.settings;
codeBlocks.forEach((codeElm, key) => {
const codeBlockFirstLine = codeBlockFirstLines[key];
const codeBlockLang = searchString(codeBlockFirstLine, "```");
const highlightedLinesParams = searchString(codeBlockFirstLine, "HL:");
const linesToHighlight = getHighlightedLines(highlightedLinesParams);
const FileName = searchString(codeBlockFirstLine, "file:");
const fileName = searchString(codeBlockFirstLine, "file:");
const Fold = searchString(codeBlockFirstLine, "fold");
const alternateColors = pluginSettings.alternateColors || [];
const lineNumberOffset = 0; //searchString(codeBlockFirstLine, "ln:")//TODO (@mayurankv) Set line number offset here - Should be ln_value - 1 since it is offset, not starting line number - 0 if true OR false
const showNumbers = true; //TODO (@mayurankv) Set showNumbers to be true if ln:<number> or ln:true or false if ln:false
const alternateColors = plugin.settings.alternateColors || [];
let altHL = [];
for (const { name, _ } of alternateColors) {
const altParams = searchString(codeBlockFirstLine, `${name}:`);
@ -211,7 +210,7 @@ function PDFExport(codeBlockElement: HTMLElement, plugin: CodeblockCustomizerPlu
}
let isCodeBlockExcluded = false;
isCodeBlockExcluded = isExcluded(codeBlockFirstLine, pluginSettings.ExcludeLangs);
isCodeBlockExcluded = isExcluded(codeBlockFirstLine, plugin.settings.ExcludeLangs);
const codeBlockPreElement: HTMLPreElement | null = codeElm.parentElement;
if (codeBlockPreElement === null) {
@ -223,6 +222,6 @@ function PDFExport(codeBlockElement: HTMLElement, plugin: CodeblockCustomizerPlu
}
const codeElements = codeBlockPreElement.getElementsByTagName("code");
AddHeaderAndHighlight(isCodeBlockExcluded, FileName, codeBlockPreElement, codeBlockLang, pluginSettings, Fold, codeElements, linesToHighlight, altHL );
AddHeaderAndHighlight(isCodeBlockExcluded, fileName, codeBlockPreElement, codeBlockLang, Fold, codeElements, showNumbers, lineNumberOffset, linesToHighlight, altHL );
})
}// PDFExport

View file

@ -1,4 +1,4 @@
export interface CodeblockCustomizerSettings {
export interface CodeblockCustomizerSettings { //TODO (@mayurankv) Refactor the settings interface and respect it
bEnableLineNumbers: boolean;
bActiveCodeblockLineHighlight: boolean;
bActiveLineHighlight: boolean;

View file

@ -259,16 +259,16 @@ function updateSettingClasses(settings) {
} else {
document.body.classList.remove("codeblock-customizer-show-line-numbers");
}
document.body.classList.remove("codeblock-customizer-show-langnames","codeblock-customizer-show-langnames-always");
if (settings.header.bAlwaysDisplayCodeblockLang) {
if (settings.header.bAlwaysDisplayCodeblockLang && settings.bDisplayCodeBlockLanguage) {
document.body.classList.add("codeblock-customizer-show-langnames-always");
} else if (settings.bDisplayCodeBlockLanguage) {
document.body.classList.add("codeblock-customizer-show-langnames");
}
document.body.classList.remove("codeblock-customizer-show-langicons","codeblock-customizer-show-langicons-always");
if (settings.header.bAlwaysDisplayCodeblockIcon) {
if (settings.header.bAlwaysDisplayCodeblockIcon && settings.bDisplayCodeBlockIcon) {
document.body.classList.add("codeblock-customizer-show-langicons-always");
} else if (settings.bDisplayCodeBlockIcon) {
document.body.classList.add("codeblock-customizer-show-langicons");
@ -281,7 +281,7 @@ function updateSettingClasses(settings) {
}
}// updateSettingStyles
function formatStyles(theme: {name: string, colors: CodeblockCustomizerColors},alternateColors) { //todo (@mayurankv) Add type hint for alternateColors
function formatStyles(theme: {name: string, colors: CodeblockCustomizerColors},alternateColors) { //TODO (@mayurankv) Add type hint for alternateColors
let current = theme['name'] == "current";
let themeClass = ''
let altHighlightStyles = ''
@ -310,7 +310,7 @@ function formatStyles(theme: {name: string, colors: CodeblockCustomizerColors},a
`;
}// formatStyles
function addAltHighlightColors(alternateColors, lightTheme: boolean) { //todo (@mayurankv) Add type hint for alternateColors
function addAltHighlightColors(alternateColors, lightTheme: boolean) { //TODO (@mayurankv) Add type hint for alternateColors
let key = lightTheme?'lightColor':'darkColor';
return alternateColors.reduce((altHighlightStyles,altHighlight) => {return altHighlightStyles + `--codeblock-customiser-highlight-${altHighlight['name'].replace(/\s+/g, '-').toLowerCase()}-color: ${altHighlight[key]};`},'')
}

View file

@ -4,6 +4,7 @@
--line-number-gutter-padding: 16px;
--border-radius: 10px;
--code-padding: 8px;
--header-padding: 5px;
}
/* Codeblock background colours */
@ -26,7 +27,8 @@
}
/* Line number gutter styling */
.codeblock-customizer:not(.codeblock-customizer-show-line-numbers) .codeblock-customizer-line-number {
.codeblock-customizer:not(.codeblock-customizer-show-line-numbers) .codeblock-customizer-line-number,
.codeblock-customizer-line-number-hide {
display: none;
}
.markdown-rendered .codeblock-customizer-pre > code > div:first-child > .codeblock-customizer-line-number { /* Maintain gutter color when padded */
@ -50,8 +52,7 @@
color: var(--codeblock-customizer-gutter-text-color);
user-select: none;
}
.markdown-source-view .codeblock-customizer-line-number,
.markdown-source-view .codeblock-customizer-line-number-specific {
.markdown-source-view .codeblock-customizer-line-number {
position: absolute;
display: inline-block;
height: 100%;
@ -60,17 +61,16 @@
direction: rtl;
left: 0;
}
.markdown-rendered .codeblock-customizer-line-number,
.markdown-rendered .codeblock-customizer-line-number-specific {
.markdown-rendered .codeblock-customizer-line-number {
flex-shrink: 0;
flex-grow: 1;
}
.codeblock-customizer-active-line-highlight.codeblock-customizer-gutter-highlight .markdown-source-view [class*="codeblock-customizer-line"].cm-active [class^='codeblock-customizer-line-number'],
.codeblock-customizer-active-line-highlight-codeblock.codeblock-customizer-gutter-highlight .markdown-source-view [class*="codeblock-customizer-line"].cm-active [class^='codeblock-customizer-line-number'],
.codeblock-customizer-gutter-highlight [class*="codeblock-customizer-line-highlighted"] [class^='codeblock-customizer-line-number'] {
.codeblock-customizer-active-line-highlight.codeblock-customizer-gutter-highlight .markdown-source-view [class*="codeblock-customizer-line"].cm-active .codeblock-customizer-line-number,
.codeblock-customizer-active-line-highlight-codeblock.codeblock-customizer-gutter-highlight .markdown-source-view [class*="codeblock-customizer-line"].cm-active .codeblock-customizer-line-number,
.codeblock-customizer-gutter-highlight [class*="codeblock-customizer-line-highlighted"] .codeblock-customizer-line-number {
background-color: rgba(0,0,0,0);
}
.codeblock-customizer.codeblock-customizer-show-line-numbers .markdown-source-view .HyperMD-codeblock:has([class^='codeblock-customizer-line-number']){
.codeblock-customizer.codeblock-customizer-show-line-numbers .markdown-source-view .HyperMD-codeblock:has(.codeblock-customizer-line-number){
padding-left: calc(var(--line-number-gutter-width) + var(--line-number-gutter-padding));
}
@ -86,6 +86,7 @@ body:not(.codeblock-customizer-show-langnames-always) .codeblock-customizer-head
.codeblock-customizer-header-container,
.codeblock-customizer-header-container-specific {
user-select: none;
padding-top: var(--header-padding);
border-top-left-radius: var(--border-radius);
border-top-right-radius: var(--border-radius);
background-color: var(--codeblock-customizer-header-background-color);
@ -93,11 +94,13 @@ body:not(.codeblock-customizer-show-langnames-always) .codeblock-customizer-head
font-size: 14px;
margin-top: 16px; /* originalVal: none, set it to 16px, so the headergets the marginTop, which was removed from the first line of the codeblock */
display: flex !important;
overflow: visible;
}
.markdown-source-view .codeblock-customizer-header-container:not(:has(+ .HyperMD-codeblock-begin)),
.markdown-source-view .codeblock-customizer-header-container-specific:not(:has(+ .HyperMD-codeblock-begin)),
.markdown-rendered .codeblock-customizer-header-container:has( + .codeblock-customizer-pre.codeblock-customizer-codeblock-collapsed),
.markdown-rendered .codeblock-customizer-header-container-specific:has( + .codeblock-customizer-pre.codeblock-customizer-codeblock-collapsed) {
padding-bottom: var(--header-padding);
border-bottom-left-radius: var(--border-radius) !important;
border-bottom-right-radius: var(--border-radius) !important;
border-bottom: none;
@ -109,17 +112,24 @@ body:not(.codeblock-customizer-show-langnames-always) .codeblock-customizer-head
}
.codeblock-customizer-header-container [class^="codeblock-customizer-header-language-tag"],
.codeblock-customizer-header-container-specific [class^="codeblock-customizer-header-language-tag"] {
height: 100%;
padding-left: 15px;
padding-right: 15px;
padding-top: 0.3em;
padding-bottom: 0.3em;
font-size: inherit;
border-radius: var(--border-radius) var(--border-radius) var(--border-radius) 0px;
border-radius: var(--border-radius) var(--border-radius) 0px 0px;
background-color: var(--codeblock-customizer-language-tag-background-color);
color: var(--codeblock-customizer-language-tag-text-color);
font-weight: var(--codeblock-customizer-language-tag-text-bold, normal);
font-style: var(--codeblock-customizer-language-tag-text-italic, normal);
}
.markdown-source-view .codeblock-customizer-header-container:not(:has(+ .HyperMD-codeblock-begin)) [class^="codeblock-customizer-header-language-tag"],
.markdown-source-view .codeblock-customizer-header-container-specific:not(:has(+ .HyperMD-codeblock-begin)) [class^="codeblock-customizer-header-language-tag"],
.markdown-rendered .codeblock-customizer-header-container:has( + .codeblock-customizer-pre.codeblock-customizer-codeblock-collapsed) [class^="codeblock-customizer-header-language-tag"],
.markdown-rendered .codeblock-customizer-header-container-specific:has( + .codeblock-customizer-pre.codeblock-customizer-codeblock-collapsed) [class^="codeblock-customizer-header-language-tag"] {
border-radius: var(--border-radius);
}
.codeblock-customizer-header-text {
padding-left: 15px;
padding-top: 0.3em;
@ -194,7 +204,7 @@ div:has(> img.codeblock-customizer-icon):has(+ [class^="codeblock-customizer-hea
border-bottom-left-radius: var(--border-radius) !important;
border-bottom-right-radius: var(--border-radius) !important;
}
.codeblock-customizer .markdown-source-view [class*="codeblock-customizer-line"].HyperMD-codeblock-end [class^='codeblock-customizer-line-number'] {
.codeblock-customizer .markdown-source-view [class*="codeblock-customizer-line"].HyperMD-codeblock-end .codeblock-customizer-line-number {
border-bottom-left-radius: var(--border-radius) !important;
}
body:not([class*='codeblock-customizer-show-langnames']) .codeblock-customizer-header-container-specific + .HyperMD-codeblock-begin,
@ -206,12 +216,12 @@ body:not(.codeblock-customizer-show-langnames-always) .codeblock-customizer-head
border-top-left-radius: var(--border-radius) !important;
border-top-right-radius: var(--border-radius) !important;
}
body:not([class*='codeblock-customizer-show-langnames']) .codeblock-customizer-header-container-specific + .HyperMD-codeblock-begin [class^='codeblock-customizer-line-number'],
body:not(.codeblock-customizer-show-langnames-always) .codeblock-customizer-header-container + .HyperMD-codeblock-begin [class^='codeblock-customizer-line-number'],
body:not([class*='codeblock-customizer-show-langicons']) .codeblock-customizer-header-container-specific + .HyperMD-codeblock-begin [class^='codeblock-customizer-line-number'],
body:not(.codeblock-customizer-show-langicons-always) .codeblock-customizer-header-container + .HyperMD-codeblock-begin [class^='codeblock-customizer-line-number'],
body:not(.codeblock-customizer-show-langicons-always.codeblock-customizer-show-langnames-always) .codeblock-customizer-header-container + .HyperMD-codeblock-begin [class^='codeblock-customizer-line-number'],
body:not(.codeblock-customizer-show-langnames-always) .codeblock-customizer-header-container:not(:has( img.codeblock-customizer-icon)) + .HyperMD-codeblock-begin [class^='codeblock-customizer-line-number'] {
body:not([class*='codeblock-customizer-show-langnames']) .codeblock-customizer-header-container-specific + .HyperMD-codeblock-begin .codeblock-customizer-line-number,
body:not(.codeblock-customizer-show-langnames-always) .codeblock-customizer-header-container + .HyperMD-codeblock-begin .codeblock-customizer-line-number,
body:not([class*='codeblock-customizer-show-langicons']) .codeblock-customizer-header-container-specific + .HyperMD-codeblock-begin .codeblock-customizer-line-number,
body:not(.codeblock-customizer-show-langicons-always) .codeblock-customizer-header-container + .HyperMD-codeblock-begin .codeblock-customizer-line-number,
body:not(.codeblock-customizer-show-langicons-always.codeblock-customizer-show-langnames-always) .codeblock-customizer-header-container + .HyperMD-codeblock-begin .codeblock-customizer-line-number,
body:not(.codeblock-customizer-show-langnames-always) .codeblock-customizer-header-container:not(:has( img.codeblock-customizer-icon)) + .HyperMD-codeblock-begin .codeblock-customizer-line-number {
border-top-left-radius: var(--border-radius);
}
body:not([class*='codeblock-customizer-show-langnames']) .codeblock-customizer-header-container-specific + .codeblock-customizer-pre,

34
toDo.md
View file

@ -1,19 +1,21 @@
# ToDo
1. Fix delay issue for opening
2. Sort css issue too large line numbers with dynamic padding in source mode
3. CSS border radius collapsed code LP and Reading
4. Clean up settings interfaces
5. Refactor to make cleaner
1. Sort settings structure and change corresponding code
6. Turn ==icons and headers== on/off via css display none and a global css class
1. icon-specific and header-specific and language-tag-specific
7. Prevent delay to render in reading mode:
@mayurankv
1. Appearance issues:
1. Too large line numbers with dynamic padding in source mode
2. Folding transition animation? CSS won't work for this.
2. Prevent delay to render in reading mode:
1. Caused by changes to HTML such as:
1. Changing highlighted lines
2. Changing file name
3. Changing line offset
4. Currently:
1. Displaying codeblock language (and always)
2. Displaying codeblock icon (and always)
8. Check startup times
- Changing highlighted lines
- Changing file name
- Presumably changing line offset
- Presumably changing fold name
3. Refactor to make cleaner
1. Sort settings structure and change corresponding code
2. Reuse functions across live preview and reading mode
4. Extra Settings
1. Placeholder text for collapsed code
1. Global placeholder in settings
2. Optional parameter after fold:
2. Let users redirect certain languages to icon of choice? Definitely not urgent if at all