docs in sub package

This commit is contained in:
Sebastian Frank 2023-02-21 13:24:14 +00:00
parent 0245b0a210
commit 29a5766d92
5 changed files with 7339 additions and 0 deletions

9
docs/docpress.json Normal file
View File

@ -0,0 +1,9 @@
{
"docs": "./",
"markdown": {
"plugins": {
"code-include": {}
}
},
"css": ["./docpress.css", "./github-dark-dimmed.css"]
}

View File

@ -0,0 +1,106 @@
const path = require('path');
const fs = require('fs');
const INCLUDE_RE = /!{3}\s*include(.+?)!{3}/i;
const BRACES_RE = /\((.+?)\)/i;
const include_plugin = (md, options) => {
const defaultOptions = {
root: '.',
getRootDir: (pluginOptions/*, state, startLine, endLine*/) => pluginOptions.root,
includeRe: INCLUDE_RE,
throwError: false,
bracesAreOptional: false,
notFoundMessage: 'File \'{{FILE}}\' not found.',
circularMessage: 'Circular reference between \'{{FILE}}\' and \'{{PARENT}}\'.'
};
if (typeof options === 'string') {
options = {
...defaultOptions,
root: options
};
} else {
options = {
...defaultOptions,
...options
};
}
const _replaceIncludeByContent = (src, rootdir, parentFilePath, filesProcessed) => {
filesProcessed = filesProcessed ? filesProcessed.slice() : []; // making a copy
let cap, filePath, mdSrc, errorMessage;
// store parent file path to check circular references
if (parentFilePath) {
filesProcessed.push(parentFilePath);
}
while ((cap = options.includeRe.exec(src))) {
let includePath = cap[1].trim();
const sansBracesMatch = BRACES_RE.exec(includePath);
if (!sansBracesMatch && !options.bracesAreOptional) {
errorMessage = `INCLUDE statement '${src.trim()}' MUST have '()' braces around the include path ('${includePath}')`;
} else if (sansBracesMatch) {
includePath = sansBracesMatch[1].trim();
} else if (!/^\s/.test(cap[1])) {
// path SHOULD have been preceeded by at least ONE whitespace character!
/* eslint max-len: "off" */
errorMessage = `INCLUDE statement '${src.trim()}': when not using braces around the path ('${includePath}'), it MUST be preceeded by at least one whitespace character to separate the include keyword and the include path.`;
}
if (!errorMessage) {
filePath = path.resolve(rootdir, includePath);
// check if child file exists or if there is a circular reference
if (!fs.existsSync(filePath)) {
// child file does not exist
errorMessage = options.notFoundMessage.replace('{{FILE}}', filePath);
} else if (filesProcessed.indexOf(filePath) !== -1) {
// reference would be circular
errorMessage = options.circularMessage.replace('{{FILE}}', filePath).replace('{{PARENT}}', parentFilePath);
}
}
// check if there were any errors
if (errorMessage) {
if (options.throwError) {
throw new Error(errorMessage);
}
mdSrc = `\n\n# INCLUDE ERROR: ${errorMessage}\n\n`;
} else {
// get content of child file
mdSrc = fs.readFileSync(filePath, 'utf8');
// check if child file also has includes
mdSrc = _replaceIncludeByContent(mdSrc, path.dirname(filePath), filePath, filesProcessed);
// remove one trailing newline, if it exists: that way, the included content does NOT
// automatically terminate the paragraph it is in due to the writer of the included
// part having terminated the content with a newline.
// However, when that snippet writer terminated with TWO (or more) newlines, these, minus one,
// will be merged with the newline after the #include statement, resulting in a 2-NL paragraph
// termination.
const len = mdSrc.length;
if (mdSrc[len - 1] === '\n') {
mdSrc = mdSrc.substring(0, len - 1);
}
}
const labelStyle = `padding: 0 4px; font-size: 12px; font-weight: bold; color: #ffffff; background: #444444; opacity: .6;`
const fileLabel = `<div style="position:relative; height: 0px;"><div class="code-file-label" style="position:absolute; top: -10px;${labelStyle}">${includePath}</div></div>\n\n`
const fileExt = includePath.replace(/^.+\./, "")
// replace include by file content
src = src.slice(0, cap.index) + fileLabel + "```" + fileExt + "\n" + mdSrc + "\n```" + src.slice(cap.index + cap[0].length, src.length);
}
return src;
};
const _includeFileParts = (state, startLine, endLine/*, silent*/) => {
state.src = _replaceIncludeByContent(state.src, options.getRootDir(options, state, startLine, endLine));
};
md.core.ruler.before('normalize', 'include', _includeFileParts);
};
module.exports = include_plugin;

View File

@ -0,0 +1,24 @@
{
"name": "markdown-it-code-include",
"version": "0.0.0",
"description": "A markdown-it plugin to include code blocks.",
"main": "./index.js",
"scripts": {
},
"keywords": [
"markdown",
"markdown-it",
"markdown-it-plugin",
"code-blocks",
"fence"
],
"license": "MIT",
"dependencies": {
"node-html-parser": "^1.3.1"
},
"devDependencies": {
"markdown-it": "^12.0.0",
"markdown-it-testgen": "^0.1.6",
"path": "^0.12.7"
}
}

19
docs/package.json Normal file
View File

@ -0,0 +1,19 @@
{
"name": "tibi-docs",
"version": "1.0.0",
"main": "README.md",
"repository": "https://gitbase.de/cms/tibi-docs",
"author": "Sebastian Frank <sebastian@webmakers.de>",
"license": "MIT",
"packageManager": "yarn@3.2.4",
"scripts": {
"docpress:serve": "docpress serve",
"docpress:build": "docpress build"
},
"devDependencies": {
"docpress": "^0.8.2"
},
"dependencies": {
"markdown-it-code-include": "./markdown-it-code-include"
}
}

7181
docs/yarn.lock Normal file

File diff suppressed because it is too large Load Diff