Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ packages/core/src/lib/markdown-it/highlight/*.js
packages/core/src/lib/markdown-it/plugins/**/*.js
packages/core/src/lib/markdown-it/utils/*.js
packages/core/src/Page/*.js
packages/core/src/patches/**/*.js
packages/core/src/plugins/**/*.js
packages/core/src/Site/*.js
packages/core/src/utils/*.js
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,14 @@ packages/core/test/unit/**/*.js
packages/core/index.js

packages/core/src/lib/progress/*.js
packages/core/src/patches/**/*.js
packages/core/src/lib/markdown-it/patches/**/*.js
# --- packages/core end ---

# Manual type definitions need to be included
!packages/cli/src/lib/live-server/index.d.ts
!packages/core/src/patches/*-internal.d.ts
!packages/core/src/patches/*-submodules.d.ts

# Nx for Lerna
.nx/cache
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
* <include>, <script>, <style>.
*/

const markdownIt = require('../../index');

const initCustomComponentHtmlBlockRule = require('./htmlBlockRule');
const htmlInlineRule = require('./htmlInlineRule');
import MarkdownIt from 'markdown-it';
import markdownIt = require('../../index');
import initCustomComponentHtmlBlockRule = require('./htmlBlockRule');
import htmlInlineRule = require('./htmlInlineRule');

/*
* MODIFIED (MarkBind): tweaked customComponentPlugin function and added injectTags function
Expand All @@ -21,7 +21,7 @@ const htmlInlineRule = require('./htmlInlineRule');
/**
* Replacing the default htmlBlock rule to allow using custom components in markdown
*/
function customComponentPlugin(md, tagsToIgnore) {
function customComponentPlugin(md: MarkdownIt, tagsToIgnore: string[]) {
const customComponentHtmlBlockRule = initCustomComponentHtmlBlockRule(tagsToIgnore);

// override default html block ruler
Expand All @@ -31,16 +31,12 @@ function customComponentPlugin(md, tagsToIgnore) {

// override default html inline ruler
md.inline.ruler.at('html_inline', htmlInlineRule);
};
}

/**
* Sets up the plugin with the provided tag names to ignore.
* Replaces any previously injected tags.
*/
function injectTags(tagsToIgnore) {
export function injectTags(tagsToIgnore: string[]) {
markdownIt.use(customComponentPlugin, tagsToIgnore);
}

module.exports = {
injectTags,
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const blockNames = require('markdown-it/lib/common/html_blocks');
const { HTML_OPEN_CLOSE_TAG_RE } = require('./htmlRe');
const inlineTags = require('./inlineTags');
import blockNames = require('markdown-it/lib/common/html_blocks');
import StateBlock from 'markdown-it/lib/rules_block/state_block';
import { HTML_OPEN_CLOSE_TAG_RE } from './htmlRe';
import inlineTags = require('./inlineTags');

function initCustomComponentHtmlBlockRule(tagsToIgnore) {
function initCustomComponentHtmlBlockRule(tagsToIgnore: string[]) {
// MODIFIED (MarkBind): Added 'special tags' and 'minimal panel' related rules

/*
Expand Down Expand Up @@ -41,7 +42,7 @@ function initCustomComponentHtmlBlockRule(tagsToIgnore) {

// An array of opening and corresponding closing sequences for html tags,
// last argument defines whether it can terminate a paragraph or not
const HTML_SEQUENCES = [
const HTML_SEQUENCES: [RegExp, RegExp, boolean][] = [
// MODIFIED (MarkBind): ignore special tags
[ startingSpecialTagRegex, endingSpecialTagRegex, true ],
[/^<!--/, /-->/, true],
Expand All @@ -65,7 +66,7 @@ function initCustomComponentHtmlBlockRule(tagsToIgnore) {
[new RegExp(HTML_OPEN_CLOSE_TAG_RE.source + '\\s*$'), /^$/, false],
]

function customComponentHtmlBlockRule(state, startLine, endLine, silent) {
function customComponentHtmlBlockRule(state: StateBlock, startLine: number, endLine: number, silent: boolean) {
let i;
let nextLine;
let lineText;
Expand Down Expand Up @@ -137,4 +138,4 @@ function initCustomComponentHtmlBlockRule(tagsToIgnore) {
return customComponentHtmlBlockRule;
}

module.exports = initCustomComponentHtmlBlockRule;
export = initCustomComponentHtmlBlockRule;
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const { HTML_TAG_RE } = require('./htmlRe');
import StateInline from 'markdown-it/lib/rules_inline/state_inline';
import { HTML_TAG_RE } from './htmlRe';

// Forked and modified from 'markdown-it/lib/rules_inline/html_inline.js'

const isLetter = (ch) => {
const isLetter = (ch: number) => {
const lc = ch | 0x20 // to lower case
return lc >= 0x61 /* a */ && lc <= 0x7a /* z */
}

const htmlInlineRule = (state, silent) => {
const htmlInlineRule = (state: StateInline, silent: boolean) => {
const pos = state.pos
if (!state.md.options.html) {
return false
Expand Down Expand Up @@ -44,4 +45,4 @@ const htmlInlineRule = (state, silent) => {
return true
}

module.exports = htmlInlineRule;
export = htmlInlineRule;
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ const HTML_OPEN_CLOSE_TAG_RE = new RegExp(
'^(?:' + open_tag + '|' + close_tag + ')'
)

module.exports = { HTML_TAG_RE, HTML_OPEN_CLOSE_TAG_RE };
export { HTML_TAG_RE, HTML_OPEN_CLOSE_TAG_RE };
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ const inlineTags = [
'wbr',
];

module.exports = inlineTags;
export = inlineTags;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// fix emoji numbers
const emojiData = require('markdown-it-emoji/lib/data/full.json');
import emojiData from 'markdown-it-emoji/lib/data/full.json';
// Extend emoji here

// Add keycap number emoji
Expand All @@ -14,4 +14,4 @@ emojiData['seven'] = '7️⃣';
emojiData['eight'] = '8️⃣';
emojiData['nine'] = '9️⃣';

module.exports = emojiData;
export = emojiData;
95 changes: 95 additions & 0 deletions packages/core/src/patches/htmlparser2-internal.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
export { }

declare module 'htmlparser2' {
interface TokenizerCallbacks {
onattribdata(value: string): void;
onattribend(): void;
onattribname(name: string): void;
oncdata(data: string): void;
oncomment(data: string): void;
ondeclaration(data: string): void;
onend(): void;
onerror(error: Error, state?: number): void;
onopentagend(): void;
onprocessinginstruction(name: string, value: string): void;
onselfclosingtag(): void;
ontext(data: string): void;
}

export class Tokenizer {
constructor(options: any, cbs: TokenizerCallbacks);
// Internal state
_state: number;
_buffer: string;
_sectionStart: number;
_index: number;
_bufferOffset: number;
_baseState: number;
_special: number;
_cbs: TokenizerCallbacks;
_running: boolean;
_ended: boolean;
_xmlMode: boolean;
_decodeEntities: boolean;

// MarkBind-added properties (set by patch)
specialTagNames: string[];
_matchingSpecialTagIndexes: number[];
_nextSpecialTagMatchIndex: number;

// Internal helpers
_getSection(): string;
_emitToken(name: string): void;
_cleanup(): void;

// State handler methods (original htmlparser2 internals)
_stateText(c: string): void;
_stateBeforeTagName(c: string): void;
_stateInTagName(c: string): void;
_stateBeforeCloseingTagName(c: string): void;
_stateInCloseingTagName(c: string): void;
_stateAfterCloseingTagName(c: string): void;
_stateBeforeAttributeName(c: string): void;
_stateInSelfClosingTag(c: string): void;
_stateInAttributeName(c: string): void;
_stateAfterAttributeName(c: string): void;
_stateBeforeAttributeValue(c: string): void;
_stateInAttributeValueDoubleQuotes(c: string): void;
_stateInAttributeValueSingleQuotes(c: string): void;
_stateInAttributeValueNoQuotes(c: string): void;
_stateBeforeDeclaration(c: string): void;
_stateInDeclaration(c: string): void;
_stateInProcessingInstruction(c: string): void;
_stateBeforeComment(c: string): void;
_stateInComment(c: string): void;
_stateAfterComment1(c: string): void;
_stateAfterComment2(c: string): void;
_stateBeforeCdata1(c: string): void;
_stateBeforeCdata2(c: string): void;
_stateBeforeCdata3(c: string): void;
_stateBeforeCdata4(c: string): void;
_stateBeforeCdata5(c: string): void;
_stateBeforeCdata6(c: string): void;
_stateInCdata(c: string): void;
_stateAfterCdata1(c: string): void;
_stateAfterCdata2(c: string): void;
_stateBeforeSpecial(c: string): void;
_stateBeforeSpecialEnd(c: string): void;
_stateBeforeEntity(c: string): void;
_stateBeforeNumericEntity(c: string): void;
_stateInNamedEntity(c: string): void;
_stateInNumericEntity(c: string): void;
_stateInHexEntity(c: string): void;

// MarkBind-added state handlers (set by patch)
_matchSpecialTagsFirstCharacters(c: string): boolean;
_matchSpecialTagsNextCharacters(c: string): number;
_matchNextSpecialTagClosingCharacter(c: string): number;
_parse(): void;
}

export interface Parser {
_attribname: string;
_closeCurrentTag(): void;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* 3. Ability to inject/whitelist certain tags to be parsed like script/style tags do. ('special' tags)
*/

const { Tokenizer, Parser } = require('htmlparser2');
import { Tokenizer, Parser } from 'htmlparser2';

/*
Enable any self closing tags '<xx />' to be parsed.
Expand All @@ -23,29 +23,11 @@ Parser.prototype.onselfclosingtag = function () {
This is equivalent to the { lowerCaseAttributeNames: false } option of htmlparser2;
We modify the relevant code to avoid passing it in repeatedly as well.
*/
Parser.prototype.onattribname = function (name) {
Parser.prototype.onattribname = function (name: string) {
this._attribname = name;
};

/* eslint-disable
brace-style,
indent,
keyword-spacing,
max-len,
no-mixed-spaces-and-tabs,
no-multi-spaces,
no-plusplus,
no-tabs,
no-unused-vars,
no-var,
one-var,
quotes,
semi,
space-before-blocks,
space-before-function-paren,
spaced-comment,
vars-on-top,
*/
/* eslint-disable */

var i = 0,

Expand Down Expand Up @@ -144,7 +126,7 @@ const DEFAULT_SPECIAL_TAGS = [
'md',
];

function whitespace(c) {
function whitespace(c: string) {
return c === " " || c === "\n" || c === "\t" || c === "\f" || c === "\r";
}

Expand All @@ -154,7 +136,7 @@ Tokenizer.prototype.specialTagNames = [...DEFAULT_SPECIAL_TAGS];
* Checks whether the token matches one of the first characters of the special tags,
* and initialises the _matchingSpecialTagIndexes array with the matches' indexes if so.
*/
Tokenizer.prototype._matchSpecialTagsFirstCharacters = function(c) {
Tokenizer.prototype._matchSpecialTagsFirstCharacters = function(c: string) {
this._matchingSpecialTagIndexes = [];
const numSpecialTags = this.specialTagNames.length;
const lowerCaseChar = c.toLowerCase();
Expand All @@ -176,7 +158,7 @@ Tokenizer.prototype._matchSpecialTagsFirstCharacters = function(c) {
* that do not match the current token.
* If one of the previous matches successfully matched, the match index is returned.
*/
Tokenizer.prototype._matchSpecialTagsNextCharacters = function(c) {
Tokenizer.prototype._matchSpecialTagsNextCharacters = function(c: string) {
const matchingSpecialTags = [];
const numMatchingTags = this._matchingSpecialTagIndexes.length;
const lowerCaseChar = c.toLowerCase();
Expand Down Expand Up @@ -205,7 +187,7 @@ Tokenizer.prototype._matchSpecialTagsNextCharacters = function(c) {
* Changes the Tokenizer state to BEFORE_SPECIAL if the token matches one of
* the first characters of _specialTagNames.
*/
Tokenizer.prototype._stateBeforeTagName = function(c) {
Tokenizer.prototype._stateBeforeTagName = function(c: string) {
if (c === "/") {
this._state = BEFORE_CLOSING_TAG_NAME;
} else if (c === "<") {
Expand All @@ -232,7 +214,7 @@ Tokenizer.prototype._stateBeforeTagName = function(c) {
* Changes the Tokenizer state to IN_TAG_NAME or BEFORE_SPECIAL state again depending
* on whether there are still matches in _matchingSpecialTagIndexes.
*/
Tokenizer.prototype._stateBeforeSpecial = function(c) {
Tokenizer.prototype._stateBeforeSpecial = function(c: string) {
const result = this._matchSpecialTagsNextCharacters(c);
if (result === HAS_MATCHING) {
this._nextSpecialTagMatchIndex += 1;
Expand Down Expand Up @@ -261,7 +243,7 @@ Tokenizer.prototype._stateBeforeSpecial = function(c) {
* Patched self closing tag state handler that removes the special state
* if the special tag was self-closed.
*/
Tokenizer.prototype._stateInSelfClosingTag = function(c) {
Tokenizer.prototype._stateInSelfClosingTag = function(c: string) {
if (c === ">") {
this._cbs.onselfclosingtag();
this._state = TEXT;
Expand All @@ -284,7 +266,7 @@ Tokenizer.prototype._stateInSelfClosingTag = function(c) {
* Processes the _special flag and _nextSpecialTagMatchIndex state variable,
* returning a flag indicating whether the current special tag has finished matching or not.
*/
Tokenizer.prototype._matchNextSpecialTagClosingCharacter = function(c) {
Tokenizer.prototype._matchNextSpecialTagClosingCharacter = function(c: string) {
const nextTestChar = this.specialTagNames[this._special - 1][this._nextSpecialTagMatchIndex];

if (nextTestChar === undefined) {
Expand All @@ -305,9 +287,10 @@ Tokenizer.prototype._matchNextSpecialTagClosingCharacter = function(c) {
* Changes the Tokenizer state to BEFORE_SPECIAL_END if the token matches one of
* the first character of the currently matched special tag.
*/
Tokenizer.prototype._stateBeforeCloseingTagName = function(c) {
if (whitespace(c));
else if (c === ">") {
Tokenizer.prototype._stateBeforeCloseingTagName = function(c: string) {
if (whitespace(c)) {
// do nothing, consume whitespace
} else if (c === ">") {
this._state = TEXT;
} else if (this._special !== SPECIAL_NONE) {
if (this._matchNextSpecialTagClosingCharacter(c) !== NO_MATCH) {
Expand All @@ -327,7 +310,7 @@ Tokenizer.prototype._stateBeforeCloseingTagName = function(c) {
* on whether the token has finished or is still matching
* the currently matched special tag.
*/
Tokenizer.prototype._stateBeforeSpecialEnd = function(c) {
Tokenizer.prototype._stateBeforeSpecialEnd = function(c: string) {
const result = this._matchNextSpecialTagClosingCharacter(c);
if (result === HAS_MATCHING) {
return;
Expand Down Expand Up @@ -478,10 +461,7 @@ Tokenizer.prototype._parse = function(){
/**
* Injects the tagsToIgnore into the Tokenizer's specialTagNames.
*/
function injectIgnoreTags(tagsToIgnore) {
export function injectIgnoreTags(tagsToIgnore: string[]) {
Tokenizer.prototype.specialTagNames = [...DEFAULT_SPECIAL_TAGS, ...tagsToIgnore];
}

module.exports = {
injectIgnoreTags,
};
Loading