mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-21 18:55:19 -04:00
feat: import markdown-it-plugins from https://github.com/hedgedoc/markdown-it-plugins
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
1d90013344
commit
f5736dad0f
37 changed files with 2025 additions and 0 deletions
98
markdown-it-plugins/src/image-size/parse-image-size.ts
Normal file
98
markdown-it-plugins/src/image-size/parse-image-size.ts
Normal file
|
@ -0,0 +1,98 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
import { SpecialCharacters } from './specialCharacters.js'
|
||||
|
||||
export interface ParseImageSize {
|
||||
position: number
|
||||
width: string
|
||||
height: string
|
||||
}
|
||||
|
||||
export interface ParseNextNumber {
|
||||
position: number
|
||||
value: string
|
||||
}
|
||||
|
||||
function isCharacterADigit(code: number) {
|
||||
return code >= SpecialCharacters.NUMBER_ZERO && code <= SpecialCharacters.NUMBER_NINE
|
||||
}
|
||||
|
||||
function findNextNotNumberCharacter(startPosition: number, maximalPosition: number, content: string): number {
|
||||
for (let position = startPosition; position < maximalPosition; position += 1) {
|
||||
const code = content.charCodeAt(position)
|
||||
if (!isCharacterADigit(code) && code !== SpecialCharacters.PERCENTAGE) {
|
||||
return position
|
||||
}
|
||||
}
|
||||
|
||||
return maximalPosition
|
||||
}
|
||||
|
||||
function parseNextNumber(content: string, startPosition: number, maximalPosition: number): ParseNextNumber {
|
||||
const endCharacterIndex = findNextNotNumberCharacter(startPosition, maximalPosition, content)
|
||||
|
||||
return {
|
||||
position: endCharacterIndex,
|
||||
value: content.slice(startPosition, endCharacterIndex)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
size must follow = without any white spaces as follows
|
||||
(1) =300x200
|
||||
(2) =300x
|
||||
(3) =x200
|
||||
*/
|
||||
const checkImageSizeStart = (code: number): boolean => {
|
||||
return (
|
||||
code === SpecialCharacters.LOWER_CASE_X ||
|
||||
(code >= SpecialCharacters.NUMBER_ZERO && code <= SpecialCharacters.NUMBER_NINE)
|
||||
)
|
||||
}
|
||||
|
||||
export function parseImageSize(
|
||||
imageSize: string,
|
||||
startCharacterPosition: number,
|
||||
maximalCharacterPosition: number
|
||||
): ParseImageSize | undefined {
|
||||
if (startCharacterPosition >= maximalCharacterPosition) {
|
||||
return
|
||||
}
|
||||
|
||||
let currentCharacterPosition = startCharacterPosition
|
||||
|
||||
if (imageSize.charCodeAt(currentCharacterPosition) !== SpecialCharacters.EQUALS /* = */) {
|
||||
return
|
||||
}
|
||||
|
||||
currentCharacterPosition += 1
|
||||
|
||||
if (!checkImageSizeStart(imageSize.charCodeAt(currentCharacterPosition))) {
|
||||
return
|
||||
}
|
||||
|
||||
// parse width
|
||||
const width = parseNextNumber(imageSize, currentCharacterPosition, maximalCharacterPosition)
|
||||
currentCharacterPosition = width.position
|
||||
|
||||
// next charactor must be 'x'
|
||||
const code = imageSize.charCodeAt(currentCharacterPosition)
|
||||
if (code !== SpecialCharacters.LOWER_CASE_X /* x */) {
|
||||
return
|
||||
}
|
||||
currentCharacterPosition += 1
|
||||
|
||||
// parse height
|
||||
const height = parseNextNumber(imageSize, currentCharacterPosition, maximalCharacterPosition)
|
||||
currentCharacterPosition = height.position
|
||||
|
||||
return {
|
||||
width: width.value,
|
||||
height: height.value,
|
||||
position: currentCharacterPosition
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue