deleting old custom plugins
This commit is contained in:
parent
4661d14912
commit
757fc55f66
|
@ -1,51 +0,0 @@
|
||||||
import { toString } from 'mdast-util-to-string';
|
|
||||||
import { visit } from 'unist-util-visit';
|
|
||||||
|
|
||||||
export default () => (tree, vFile) => {
|
|
||||||
let headers = [];
|
|
||||||
visit(tree, (node) => {
|
|
||||||
if (node.type === 'heading') {
|
|
||||||
if (node.depth !== 1) {
|
|
||||||
const sanitizedString = toString(node).replace(
|
|
||||||
/{(.*?)}/,
|
|
||||||
(_, token) => {
|
|
||||||
return vFile.data.fm[token] || token;
|
|
||||||
},
|
|
||||||
);
|
|
||||||
headers.push({
|
|
||||||
title: sanitizedString,
|
|
||||||
level: node.depth,
|
|
||||||
slug: node.data.id,
|
|
||||||
children: [],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const stack = [];
|
|
||||||
const sortedHeaders = [];
|
|
||||||
|
|
||||||
//const sort Header to tree
|
|
||||||
const sortHeader = (header) => {
|
|
||||||
while (stack.length !== 0 && header.level <= stack[0].level) {
|
|
||||||
stack.shift();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stack.length === 0) {
|
|
||||||
sortedHeaders.push(header);
|
|
||||||
stack.push(header);
|
|
||||||
} else {
|
|
||||||
(stack[0].children ??= []).push(header);
|
|
||||||
stack.unshift(header);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
headers.forEach((_, index) => {
|
|
||||||
const header = headers[index];
|
|
||||||
sortHeader(header);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!vFile.data.fm) vFile.data.fm = {};
|
|
||||||
vFile.data.fm.headings = sortedHeaders;
|
|
||||||
return tree;
|
|
||||||
};
|
|
|
@ -1,45 +0,0 @@
|
||||||
import Prism from 'prismjs';
|
|
||||||
import 'prismjs/components/prism-docker.min.js';
|
|
||||||
import 'prismjs/components/prism-bash.min.js';
|
|
||||||
import 'prismjs/components/prism-yaml.min.js';
|
|
||||||
import 'prismjs/components/prism-javascript.min.js';
|
|
||||||
import 'prismjs/components/prism-json.min.js';
|
|
||||||
import 'prismjs/components/prism-markdown.min.js';
|
|
||||||
import 'prismjs/components/prism-sql.min.js';
|
|
||||||
import 'prismjs/components/prism-toml.min.js';
|
|
||||||
import 'prismjs/components/prism-promql.min.js';
|
|
||||||
import 'prismjs/components/prism-go.min.js';
|
|
||||||
import 'prismjs/components/prism-typescript.min.js';
|
|
||||||
import 'prismjs/components/prism-python.min.js';
|
|
||||||
import { escapeSvelte } from 'mdsvex';
|
|
||||||
|
|
||||||
const langMap = {
|
|
||||||
sh: 'bash',
|
|
||||||
Dockerfile: 'dockerfile',
|
|
||||||
YAML: 'yaml',
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param {string} code the code that gets parsed
|
|
||||||
* @param {string} lang the language the code is written in
|
|
||||||
* @param {string} meta meta information for the code fence
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
export function highlightCode(code, lang, meta) {
|
|
||||||
let title = null;
|
|
||||||
const _lang = langMap[lang] || lang || '';
|
|
||||||
|
|
||||||
if (meta) {
|
|
||||||
title = meta.match(/title="?(.*?)"/)?.[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
const highlighted = _lang
|
|
||||||
? escapeSvelte(Prism.highlight(code, Prism.languages[_lang], _lang))
|
|
||||||
: code;
|
|
||||||
return `<CodeFence code={${JSON.stringify(highlighted)}}
|
|
||||||
rawCode={${JSON.stringify(code)}}
|
|
||||||
lang={"${_lang}"}
|
|
||||||
${title ? `title={"${title}"}` : ''}
|
|
||||||
/>`;
|
|
||||||
}
|
|
|
@ -1,73 +0,0 @@
|
||||||
/**
|
|
||||||
* Credit goes to @Xananax for providing this solution within the gist https://gist.github.com/Xananax/5dca3a1dd7070e4fdebe2927e4aeb55b
|
|
||||||
*/
|
|
||||||
import { join, basename, extname } from 'path';
|
|
||||||
|
|
||||||
export const defaults = {
|
|
||||||
extensions: ['.svelte.md', '.md', '.svx'],
|
|
||||||
dir: `$lib`,
|
|
||||||
list: [],
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Injects global imports in all your mdsvex files
|
|
||||||
* Specify:
|
|
||||||
* - the root dir (defaults to `src/lib`)
|
|
||||||
* - the array list of components (with extension), like `['Component.svelte']`
|
|
||||||
* - the valid extensions list as an array (defaults to `['.svelte.md', '.md', '.svx']`)
|
|
||||||
*
|
|
||||||
* If you want the component name to be different from the file name, you can specify an array
|
|
||||||
* of arrays: `['Component.svelte', ['Another', 'AnotherComp.svelte'], 'ThirdComp.svelte']`
|
|
||||||
*
|
|
||||||
* @param {Object} options options described above
|
|
||||||
* @returns a preprocessor suitable to plug into the `preprocess` key of the svelte config
|
|
||||||
*/
|
|
||||||
export const mdsvexGlobalComponents = (options = {}) => {
|
|
||||||
const { extensions, dir, list } = { ...defaults, ...options };
|
|
||||||
const extensionsRegex = new RegExp(
|
|
||||||
'(' + extensions.join('|').replace(/\./g, '\\.') + ')$',
|
|
||||||
'i',
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!list || !list.length || !Array.isArray(list)) {
|
|
||||||
throw new Error(
|
|
||||||
`"list" option must be an array and contain at least one element`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const imports = list
|
|
||||||
.map((entry) => {
|
|
||||||
let name = '';
|
|
||||||
if (Array.isArray(entry)) {
|
|
||||||
name = entry[0];
|
|
||||||
entry = entry[1];
|
|
||||||
}
|
|
||||||
const ext = extname(entry);
|
|
||||||
const path = join(dir, entry);
|
|
||||||
name = name || basename(entry, ext);
|
|
||||||
return `\nimport ${name} from "${path}"`;
|
|
||||||
})
|
|
||||||
.join('\n');
|
|
||||||
|
|
||||||
const preprocessor = {
|
|
||||||
script(thing) {
|
|
||||||
const { content, filename, attributes, markup } = thing;
|
|
||||||
if (!filename.match(extensionsRegex)) {
|
|
||||||
return { code: content };
|
|
||||||
}
|
|
||||||
const hasModuleContext = /^<script context="module">/.test(markup);
|
|
||||||
|
|
||||||
const isModulePass = attributes?.context === 'module';
|
|
||||||
if (!isModulePass || !hasModuleContext) {
|
|
||||||
return { code: content };
|
|
||||||
}
|
|
||||||
const isValidPass =
|
|
||||||
(hasModuleContext && isModulePass) || !hasModuleContext;
|
|
||||||
if (!isValidPass) {
|
|
||||||
return { code: content };
|
|
||||||
}
|
|
||||||
return { code: `${imports}\n${content}` };
|
|
||||||
},
|
|
||||||
};
|
|
||||||
return preprocessor;
|
|
||||||
};
|
|
|
@ -1,70 +0,0 @@
|
||||||
/**
|
|
||||||
* Credit goes to @Xananax for providing this solution within the gist https://gist.github.com/Xananax/5dca3a1dd7070e4fdebe2927e4aeb55b
|
|
||||||
*/
|
|
||||||
import { join, basename, extname } from 'path';
|
|
||||||
|
|
||||||
export const defaults = {
|
|
||||||
extensions: ['.svelte.md', '.md', '.svx'],
|
|
||||||
dir: `$lib`,
|
|
||||||
list: []
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Injects global imports in all your mdsvex files
|
|
||||||
* Specify:
|
|
||||||
* - the root dir (defaults to `src/lib`)
|
|
||||||
* - the array list of components (with extension), like `['Component.svelte']`
|
|
||||||
* - the valid extensions list as an array (defaults to `['.svelte.md', '.md', '.svx']`)
|
|
||||||
*
|
|
||||||
* If you want the component name to be different from the file name, you can specify an array
|
|
||||||
* of arrays: `['Component.svelte', ['Another', 'AnotherComp.svelte'], 'ThirdComp.svelte']`
|
|
||||||
*
|
|
||||||
* @param {Object} options options described above
|
|
||||||
* @returns a preprocessor suitable to plug into the `preprocess` key of the svelte config
|
|
||||||
*/
|
|
||||||
export const mdsvexGlobalComponents = (options = {}) => {
|
|
||||||
const { extensions, dir, list } = { ...defaults, ...options };
|
|
||||||
const extensionsRegex = new RegExp(
|
|
||||||
'(' + extensions.join('|').replace(/\./g, '\\.') + ')$',
|
|
||||||
'i'
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!list || !list.length || !Array.isArray(list)) {
|
|
||||||
throw new Error(`"list" option must be an array and contain at least one element`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const imports = list
|
|
||||||
.map((entry) => {
|
|
||||||
let name = '';
|
|
||||||
if (Array.isArray(entry)) {
|
|
||||||
name = entry[0];
|
|
||||||
entry = entry[1];
|
|
||||||
}
|
|
||||||
const ext = extname(entry);
|
|
||||||
const path = join(dir, entry);
|
|
||||||
name = name || basename(entry, ext);
|
|
||||||
return `\nimport ${name} from "${path}";`;
|
|
||||||
})
|
|
||||||
.join('\n');
|
|
||||||
|
|
||||||
const preprocessor = {
|
|
||||||
script(thing) {
|
|
||||||
const { content, filename, attributes, markup } = thing;
|
|
||||||
if (!filename.match(extensionsRegex)) {
|
|
||||||
return { code: content };
|
|
||||||
}
|
|
||||||
const hasModuleContext = /^<script context="module">/.test(markup);
|
|
||||||
|
|
||||||
const isModulePass = attributes?.context === 'module';
|
|
||||||
if (!isModulePass || !hasModuleContext) {
|
|
||||||
return { code: content };
|
|
||||||
}
|
|
||||||
const isValidPass = (hasModuleContext && isModulePass) || !hasModuleContext;
|
|
||||||
if (!isValidPass) {
|
|
||||||
return { code: content };
|
|
||||||
}
|
|
||||||
return { code: `${imports}\n${content}` };
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return preprocessor;
|
|
||||||
};
|
|
|
@ -1,30 +0,0 @@
|
||||||
import { visit } from 'unist-util-visit';
|
|
||||||
|
|
||||||
const youtubeEmbedRegex = new RegExp(`\(youtube\):\(\.\*\)`, 'i');
|
|
||||||
|
|
||||||
const embedVideoHtml = (videoId, options) => `
|
|
||||||
<span class="relative mt-medium not-prose block overflow-hidden max-w-full after:block after:pt-[56.26%]"><iframe
|
|
||||||
title="${options.title ? options.title : 'Youtube Video'}"
|
|
||||||
width="${options.width}"
|
|
||||||
height="${options.height}"
|
|
||||||
src="https://www.youtube-nocookie.com/embed/${videoId}?rel=0"
|
|
||||||
class="absolute top-0 left-0 w-full h-full"
|
|
||||||
${options.noIframeBorder ? 'style="border:0"' : ''}
|
|
||||||
allowfullscreen
|
|
||||||
sandbox="allow-same-origin allow-scripts allow-popups">
|
|
||||||
</iframe></span>`;
|
|
||||||
|
|
||||||
const visitor = (options) => (node) => {
|
|
||||||
if (node.type === 'inlineCode') {
|
|
||||||
const regexResult = node.value.match(youtubeEmbedRegex);
|
|
||||||
if (regexResult) {
|
|
||||||
node.type = 'html';
|
|
||||||
node.value = embedVideoHtml(regexResult[2].trim(), options);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export default (options) => (tree) => {
|
|
||||||
visit(tree, visitor(options));
|
|
||||||
return tree;
|
|
||||||
};
|
|
|
@ -1,34 +0,0 @@
|
||||||
import { visit } from 'unist-util-visit';
|
|
||||||
import regexCreator from 'emoji-regex';
|
|
||||||
const emojiRegex = regexCreator();
|
|
||||||
|
|
||||||
const nonAlphanumericsAtTheBeginningRegex = /^\W+/g;
|
|
||||||
const nonAlphanumericsAtTheEndRegex = /\W+$/g;
|
|
||||||
|
|
||||||
const beautifyFragment = (str = '') =>
|
|
||||||
str
|
|
||||||
.replace(emojiRegex, '')
|
|
||||||
.replace(nonAlphanumericsAtTheBeginningRegex, '')
|
|
||||||
.replace(nonAlphanumericsAtTheEndRegex, '');
|
|
||||||
|
|
||||||
const visitor = (node) => {
|
|
||||||
node.data = node.data || {};
|
|
||||||
node.data.hProperties = node.data.hProperties || {};
|
|
||||||
|
|
||||||
if (node.type === 'heading') {
|
|
||||||
let fragment = node.data.id;
|
|
||||||
fragment = beautifyFragment(fragment);
|
|
||||||
|
|
||||||
const lastChildrenIdx = node.children.length - 1;
|
|
||||||
const headingPermalink = node.children[lastChildrenIdx];
|
|
||||||
headingPermalink.url = `#${fragment}`;
|
|
||||||
|
|
||||||
node.data.hProperties.id = fragment;
|
|
||||||
node.data.id = fragment;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export default () => async (tree) => {
|
|
||||||
visit(tree, visitor);
|
|
||||||
return tree;
|
|
||||||
};
|
|
Loading…
Reference in New Issue