config.ts 9.34 KB
Newer Older
1 2

import { configuration } from "@codedoc/core";
3
import { GlobalScripts } from "./plugins/global-scripts";
4 5
import { theme } from "./theme";

Tom Riedl's avatar
Tom Riedl committed
6 7
const fse = require("fs-extra");
const mergeFiles = require('merge-files');
8 9 10 11 12 13

export const config = /*#__PURE__*/configuration({
    theme,

    page: {
        title: {
Tom Riedl's avatar
Tom Riedl committed
14
            base: "Tom Riedl",
15
            connector: " | "
Tom Riedl's avatar
Tom Riedl committed
16 17 18
        },
        meta: {
            subject: 'Tom Riedl',
Tom Riedl's avatar
Tom Riedl committed
19
            description: 'Business, Trading, Technik, Programmieren, Software, Kryptowährungen',
Tom Riedl's avatar
Tom Riedl committed
20 21 22 23
            keywords: ['tom', 'riedl', 'tomriedl', 'binance', 'bitfinex', 'crypto', 'krypto', 'referral', 'affiliate', 'system', 'code', 'partner', 'kryptowährungen', 'apps', 'telegram', 'android', 'ios'],
            themeColor: "#212121", // --> the browser bar color of your docs
            appleMobileWebStatusBarStyle: "black-translucent"
        },
24
        favicon: "/favicon.ico",
Tom Riedl's avatar
Tom Riedl committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
        stylesheets: [ ],
        fonts: {
            text: {
                name: "Hind",
                url: "",
                fallback: "sans-serif"
            },
            code: {
                name: "Source Code Pro",
                url: "",
                fallback: `'Courier New', Courier, monospace`
            },
            icon: {
                name: "Material Icons",
                url: "",
                outline: "Material Icons Outlined"
            },
42 43 44
        }
    },

Tom Riedl's avatar
Tom Riedl committed
45 46 47 48 49 50 51
    src: {
        base: 'docs/md',
        toc: '_toc.md',
        not_found: '404.md',
        pick: /\.md$/,
    },

52 53
    dest: {
        assets: "dist",
54 55
        bundle: "bundle",
        styles: "styles",
56 57 58
        html: "dist"
    },

59 60 61 62
    plugins: [
        GlobalScripts()
    ],

63
    afterBuild: [
Tom Riedl's avatar
Tom Riedl committed
64
        async function Minify(): Promise<void>
65
        {
Tom Riedl's avatar
Tom Riedl committed
66
            const source: string = "docs/assets/styles.css";
67 68
            const destination: string = "dist/styles/codedoc-styles.css";
            const merged: string = "dist/styles/styles-merged.css";
Tom Riedl's avatar
Tom Riedl committed
69 70 71 72 73 74 75
            await mergeFiles([destination, source], merged);
            await fse.move(merged, destination, { overwrite: true }, (error: any) => { if (error) { console.error(error); } });
        },
        async function CopyAssets(): Promise<void>
        {
            await fse.copy("docs/assets", "dist/assets", { overwrite: true }, (error: any) => { if (error) { console.error(error); } });
            await fse.copy("docs/fonts", "dist/fonts", { overwrite: true }, (error: any) => { if (error) { console.error(error); } });
Tom Riedl's avatar
Tom Riedl committed
76
            await fse.copy("docs/root", "dist", { overwrite: true }, (error: any) => { if (error) { console.error(error); } });
77
        },
Tom Riedl's avatar
Tom Riedl committed
78
        async function CopyVersion1Content(): Promise<void>
79
        {
Tom Riedl's avatar
Tom Riedl committed
80
            await fse.copy("docs/wp-content", "dist/wp-content", { overwrite: true }, (error: any) => { if (error) { console.error(error); } });
Tom Riedl's avatar
Tom Riedl committed
81
        },
Tom Riedl's avatar
Tom Riedl committed
82
        async function FileOperations(): Promise<void>
Tom Riedl's avatar
Tom Riedl committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
        {
            const { resolve } = require('path');
            const { readdir } = require('fs').promises;
            async function* getFiles(path: string): any
            {
                const directories = await readdir(path, { withFileTypes: true });
                for (const directory of directories)
                {
                    const res = resolve(path, directory.name);
                    if (directory.isDirectory())
                    {
                        yield* getFiles(res);
                    }
                    else
                    {
                        yield res;
                    }
                }
            }

Tom Riedl's avatar
Tom Riedl committed
103
            async function PostProcessHtml(filepath: string): Promise<void>
Tom Riedl's avatar
Tom Riedl committed
104 105 106 107 108
            {
                fse.readFile(filepath, "utf8", function (error: any, data: any)
                {
                    if (error)
                    {
Tom Riedl's avatar
Tom Riedl committed
109
                        return console.error("Unable to read file in AddLangToHtml.", error);
Tom Riedl's avatar
Tom Riedl committed
110
                    }
111
                    let result: string = data.replace(/<html/g, '<html lang="de" prefix="og:http://ogp.me/ns#"');
Tom Riedl's avatar
Tom Riedl committed
112 113
                    result = result.replace(/, user-scalable=0/g, '');
                    result = result.replace(/maximum-scale=1/g, 'maximum-scale=5');
Tom Riedl's avatar
Tom Riedl committed
114 115
                    result = result.replace(/<a target="_blank">See/g, '<a href="javascript:void(0);" target="_blank" rel="nofollow">See');
                    result = result.replace(/target="_blank"/g, 'target="_blank" rel="noopener"');
Tom Riedl's avatar
Tom Riedl committed
116 117 118 119 120
                    result = result.replace(/class="header-0-0/g, 'class="header-0-0-x header-0-0');
                    result = result.replace(/class="toc-0-0/g, 'class="toc-0-0-x toc-0-0');
                    result = result.replace(/class="heading-0-0/g, 'class="heading-0-0-x heading-0-0');
                    result = result.replace(/class="anchor-0-0/g, 'class="anchor-0-0-x anchor-0-0');
                    result = result.replace(/class="contentnav-0-0/g, 'class="contentnav-0-0-x contentnav-0-0');
Tom Riedl's avatar
Tom Riedl committed
121

122 123 124 125 126
                    result = result.replace(/BINANCE_URL/g, 'https://www.binance.com/de/register?ref=MEA4TAZZ');
                    result = result.replace(/BITFINEX_URL/g, 'https://www.bitfinex.com/?refcode=yiZ39X8ke');
                    result = result.replace(/POLONIEX_URL/g, 'https://poloniex.com/signup?c=P3DCTAWA');
                    result = result.replace(/COINSPOT_URL/g, 'https://www.coinspot.com.au?affiliate=3RQEC');
                    result = result.replace(/OKEX_URL/g, 'https://www.okex.com/join/1/3066896');
127
                    result = result.replace(/SERVERPROFIS_URL/g, 'https://service.serverprofis.net/aff.php?aff=846');
128

129 130 131 132
                    // Free title tag to read it easily from shell script
                    result = result.replace("<title>", "\n<title>");
                    result = result.replace("</title>", "</title>\n");

133
                    // Open all external links in new tabs
134 135
                    const linkTags: RegExpMatchArray | null = result.match(/<a [^>]*href="[^"]*"[^>]*>/gm);
                    for (const tag of linkTags || [])
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
                    {
                        const uri: RegExpMatchArray | null = tag.match(/.*href="([^"]*)".*/);
                        if (uri && uri.length >= 2)
                        {
                            const href: string = uri[1];
                            if (href && href[0] != "/" && href[0] != "#" && href.indexOf("https://tomriedl.com") < 0
                                && tag.lastIndexOf("\">") == tag.length - 2)
                            {
                                const search: string = tag.substring(0, tag.length - 2);
                                const replace: string = `${search}" rel="nofollow" onclick="window.open(this.href);return false`;
                                result = result.replace(search, replace);
                            }
                        }
                    }

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
                    // Replace tags
                    const tagsCode: RegExpMatchArray | null = result.match(/<code>##Tags##:(.*?)<\/code>/gm);
                    for (const codeBlock of tagsCode || [])
                    {
                        const tags: string = codeBlock.replace("<code>##Tags##:", "").replace("</code>", "").replace(",", " ").trim();
                        const tagList: Array<string> = tags.toLowerCase().split(" ");
                        const replacementTags: Array<string> = [];
                        for (const rawTag of tagList)
                        {
                            const tag = rawTag.trim();
                            replacementTags.push(`<a href="/tag/${tag}" class="TagLink">${tag}</a>`);
                        }
                        const replacement: string = `Tags: \n${replacementTags.join(" &middot; \n")}\n`;
                        result = result.replace(codeBlock, replacement);
                    }

167 168
                    // Extract first image to add it to og:image
                    const imageUrls: Array<string> | undefined = result.match(/<img [^>]*src="[^"]*"[^>]*>/gm)?.map((x: string) => x.replace(/.*src="([^"]*)".*/, '$1'));
Tom Riedl's avatar
Tom Riedl committed
169 170 171 172 173 174
                    const coverImage: string = imageUrls && imageUrls.length ? imageUrls[0] : "";
                    if (coverImage)
                    {
                        result = result.replace('<link ', `<meta property="og:image" content="${coverImage}"><link `);
                    }

Tom Riedl's avatar
Tom Riedl committed
175 176 177 178 179 180 181
                    fse.writeFile(filepath, result, "utf8", function (error: any)
                    {
                        if (error) return console.error(error);
                    });
                });
            }

Tom Riedl's avatar
Tom Riedl committed
182 183 184 185 186 187 188 189 190 191 192 193 194
            for await (const filepath of getFiles("docs/md"))
            {
                const extension: string = filepath.split(".").pop();
                
                switch (extension)
                {
                    case "png":
                    case "jpg":
                        await fse.copy(filepath, filepath.replace("/docs/md/", "/dist/"), { overwrite: true }, (error: any) => { if (error) { console.error(error); } });
                        break;
                }
            }

Tom Riedl's avatar
Tom Riedl committed
195 196 197 198 199 200 201
            for await (const filepath of getFiles("dist"))
            {
                const extension: string = filepath.split(".").pop();
                
                switch (extension)
                {
                    case "html":
Tom Riedl's avatar
Tom Riedl committed
202
                        await PostProcessHtml(filepath);
Tom Riedl's avatar
Tom Riedl committed
203 204 205
                        break;
                }
            }
206 207 208 209
        }
    ],
    
});