mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
1 parent
835ff8d428
commit
484729cdbe
4 files changed
+502
No files matched your search
@@ -0,0 +1,13 @@
|
||||
import { AlignJustified } from '@vicons/tabler';
|
||||
import { defineTool } from '../tool';
|
||||
|
||||
export const tool = defineTool({
|
||||
name: 'Markdown Lorem Ipsum',
|
||||
path: '/markdown-lorem-ipsum',
|
||||
description: 'Generate Lorem Ipsum in markdown',
|
||||
keywords: ['markdown', 'lorem', 'ipsum'],
|
||||
component: () => import('./markdown-lorem-ipsum.vue'),
|
||||
icon: AlignJustified,
|
||||
createdAt: new Date('2025-11-29'),
|
||||
category: 'Markdown',
|
||||
});
|
||||
@@ -0,0 +1,90 @@
|
||||
// tests/generator.spec.ts
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { type GeneratorConfig, type HeaderStyle, generateMarkdown } from './markdown-lorem-ipsum.service';
|
||||
|
||||
const baseCfg: GeneratorConfig = {
|
||||
seedStr: 'seed-paris-2025',
|
||||
blocks: 8,
|
||||
avgSentencePerPara: 4,
|
||||
enableHeaders: true,
|
||||
enableLists: true,
|
||||
enableCode: true,
|
||||
enableBlockquotes: true,
|
||||
inlineEmphasis: true,
|
||||
inlineStrong: true,
|
||||
inlineLinks: true,
|
||||
inlineCode: true,
|
||||
headerStyle: 'atx',
|
||||
listStyle: 'unordered',
|
||||
headerFrequency: 0.25,
|
||||
listFrequency: 0.25,
|
||||
codeFrequency: 0.15,
|
||||
quoteFrequency: 0.15,
|
||||
language: 'English',
|
||||
};
|
||||
|
||||
describe('generateMarkdown', () => {
|
||||
it('is deterministic for a given seed and config', () => {
|
||||
const a = generateMarkdown(baseCfg);
|
||||
const b = generateMarkdown({ ...baseCfg });
|
||||
expect(a).toEqual(b);
|
||||
});
|
||||
|
||||
it('changes when seed changes', () => {
|
||||
const a = generateMarkdown(baseCfg);
|
||||
const b = generateMarkdown({ ...baseCfg, seedStr: 'seed-paris-2026' });
|
||||
expect(a).not.toEqual(b);
|
||||
});
|
||||
|
||||
it('respects block count', () => {
|
||||
const cfg = { ...baseCfg, blocks: 12 };
|
||||
const out = generateMarkdown(cfg);
|
||||
const blocks = out.split(/\n{2,}/);
|
||||
expect(blocks.length).toBeGreaterThanOrEqual(12); // footnotes may add extra
|
||||
});
|
||||
|
||||
it('emits ATX headers when configured', () => {
|
||||
const cfg = { ...baseCfg, headerStyle: 'atx' as HeaderStyle, seedStr: 'h-atx' };
|
||||
const out = generateMarkdown(cfg);
|
||||
expect(out).toMatch(/^#{1,6}\s.+/m);
|
||||
});
|
||||
|
||||
it('emits setext headers when configured', () => {
|
||||
const cfg = { ...baseCfg, headerStyle: 'setext' as HeaderStyle, seedStr: 'h-setext' };
|
||||
const out = generateMarkdown(cfg);
|
||||
// Look for underline of = or - on next line
|
||||
expect(out).toMatch(/^[A-Z][^\n]+\n[=-]+$/m);
|
||||
});
|
||||
|
||||
it('emits ordered and unordered lists per config', () => {
|
||||
const unordered = generateMarkdown({ ...baseCfg, listStyle: 'unordered', seedStr: 'list-u' });
|
||||
expect(unordered).toMatch(/^\s*[-*+]\s.+/m);
|
||||
|
||||
const ordered = generateMarkdown({ ...baseCfg, listStyle: 'ordered', seedStr: 'list-o' });
|
||||
expect(ordered).toMatch(/^\s*\d+\.\s.+/m);
|
||||
});
|
||||
|
||||
it('contains inline decorations when enabled', () => {
|
||||
const out = generateMarkdown({ ...baseCfg, seedStr: 'inline-on' });
|
||||
expect(out).toMatch(/`[^`\n]+`|\*\*[^\*\n]+\*\*|\*[^\*\n]+\*|\[[^\]]+\]\([^)]+\)/);
|
||||
});
|
||||
|
||||
it('disables inline decorations when toggled off', () => {
|
||||
const out = generateMarkdown({
|
||||
...baseCfg,
|
||||
seedStr: 'inline-off',
|
||||
inlineEmphasis: false,
|
||||
inlineStrong: false,
|
||||
inlineLinks: false,
|
||||
inlineCode: false,
|
||||
});
|
||||
expect(out).not.toMatch(/`[^`\n]+`|\*\*[^\*\n]+\*\*|\*[^\*\n]+\*|\[[^\]]+\]\([^)]+\)/);
|
||||
});
|
||||
|
||||
it('can be skewed towards headers via frequency', () => {
|
||||
const cfg = { ...baseCfg, seedStr: 'freq-headers', headerFrequency: 0.8, listFrequency: 0.05, codeFrequency: 0.05, quoteFrequency: 0.05 };
|
||||
const out = generateMarkdown(cfg);
|
||||
const headers = out.match(/^#{1,6}\s.+|^[A-Z][^\n]+\n[=-]+$/gm) ?? [];
|
||||
expect(headers.length).toBeGreaterThan(3);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,251 @@
|
||||
import _ from 'lodash';
|
||||
import languageLorems from '../lorem-ipsum-generator/lorem-ipsum.i18n.json';
|
||||
|
||||
export type HeaderStyle = 'atx' | 'setext';
|
||||
export type ListStyle = 'unordered' | 'ordered';
|
||||
|
||||
export interface GeneratorConfig {
|
||||
seedStr: string
|
||||
blocks: number
|
||||
avgSentencePerPara: number
|
||||
enableHeaders: boolean
|
||||
enableLists: boolean
|
||||
enableCode: boolean
|
||||
enableBlockquotes: boolean
|
||||
inlineEmphasis: boolean
|
||||
inlineStrong: boolean
|
||||
inlineLinks: boolean
|
||||
inlineCode: boolean
|
||||
headerStyle: HeaderStyle
|
||||
listStyle: ListStyle
|
||||
headerFrequency: number
|
||||
listFrequency: number
|
||||
codeFrequency: number
|
||||
quoteFrequency: number
|
||||
language: string
|
||||
}
|
||||
|
||||
export function getSupportedLanguages() {
|
||||
return _.flatten(_.chain(languageLorems).map(l => l.languages).value()).sort();
|
||||
}
|
||||
|
||||
export function hashSeed(str: string): number {
|
||||
let h = 2166136261 >>> 0;
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
h ^= str.charCodeAt(i);
|
||||
h = Math.imul(h, 16777619);
|
||||
}
|
||||
return (h >>> 0);
|
||||
}
|
||||
|
||||
export function mulberry32(a: number) {
|
||||
return function () {
|
||||
let t = (a += 0x6D2B79F5);
|
||||
t = Math.imul(t ^ (t >>> 15), t | 1);
|
||||
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
||||
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
||||
};
|
||||
}
|
||||
|
||||
type Chain = Record<string, string[]>;
|
||||
function buildChain(words: string[]): Chain {
|
||||
const chain: Chain = {};
|
||||
for (let i = 0; i < words.length - 1; i++) {
|
||||
const w = words[i];
|
||||
const next = words[i + 1];
|
||||
if (!chain[w]) {
|
||||
chain[w] = [];
|
||||
}
|
||||
chain[w].push(next);
|
||||
}
|
||||
const last = words[words.length - 1];
|
||||
if (!chain[last]) {
|
||||
chain[last] = [];
|
||||
}
|
||||
chain[last].push(words[0]);
|
||||
return chain;
|
||||
}
|
||||
|
||||
function nextWord(chain: Chain, current: string, rnd: () => number, latinWords: string[]): string {
|
||||
const candidates = chain[current] ?? latinWords;
|
||||
return candidates[Math.floor(rnd() * candidates.length)];
|
||||
}
|
||||
|
||||
function getLoremWords(cfg: GeneratorConfig) {
|
||||
return _.find(languageLorems, ({ languages }) => languages.includes(cfg.language))?.loremIpsum?.split(' ') || [];
|
||||
}
|
||||
|
||||
function capitalized(word: string): string {
|
||||
return word.charAt(0).toUpperCase() + word.slice(1);
|
||||
}
|
||||
|
||||
function sentence(cfg: GeneratorConfig, chain: Chain, latinWords: string[], rnd: () => number, min = 6, max = 16): string {
|
||||
const len = Math.floor(rnd() * (max - min + 1)) + min;
|
||||
let w = latinWords[Math.floor(rnd() * latinWords.length)];
|
||||
const parts: string[] = [];
|
||||
for (let i = 0; i < len; i++) {
|
||||
let token = w;
|
||||
if (cfg.inlineCode && rnd() < 0.05) {
|
||||
token = `\`${token}\``;
|
||||
}
|
||||
if (cfg.inlineEmphasis && rnd() < 0.08) {
|
||||
token = `*${token}*`;
|
||||
}
|
||||
if (cfg.inlineStrong && rnd() < 0.06) {
|
||||
token = `**${token}**`;
|
||||
}
|
||||
if (cfg.inlineLinks && rnd() < 0.05) {
|
||||
token = `[${token}](https://example.com/${token})`;
|
||||
}
|
||||
parts.push(token);
|
||||
w = nextWord(chain, w, rnd, latinWords);
|
||||
}
|
||||
const body = parts.join(' ');
|
||||
const end = rnd() < 0.1 ? '?!' : (rnd() < 0.5 ? '.' : '!');
|
||||
return capitalized(body) + end;
|
||||
}
|
||||
|
||||
function paragraph(cfg: GeneratorConfig, chain: Chain, rnd: () => number, latinWords: string[]): string {
|
||||
const sCount = Math.max(1, Math.round(cfg.avgSentencePerPara + (rnd() - 0.5) * 2));
|
||||
return Array.from({ length: sCount }).map(() => sentence(cfg, chain, latinWords, rnd)).join(' ');
|
||||
}
|
||||
|
||||
function header(rnd: () => number, level: number, style: HeaderStyle, latinWords: string[]): string {
|
||||
const pick = () => capitalized(latinWords[Math.floor(rnd() * latinWords.length)]);
|
||||
const title = `${pick()} ${pick()} ${pick()}`;
|
||||
if (style === 'atx') {
|
||||
return `${'#'.repeat(Math.min(level, 6))} ${title}`;
|
||||
}
|
||||
else {
|
||||
const underline = level <= 1 ? '='.repeat(title.length) : '-'.repeat(title.length);
|
||||
return `${title}\n${underline}`;
|
||||
}
|
||||
}
|
||||
|
||||
function listBlock(cfg: GeneratorConfig, rnd: () => number, latinWords: string[]): string {
|
||||
const items = Math.floor(rnd() * 5) + 3;
|
||||
const lines: string[] = [];
|
||||
const chain = buildChain(latinWords);
|
||||
for (let i = 0; i < items; i++) {
|
||||
const marker = cfg.listStyle === 'ordered'
|
||||
? `${i + 1}.`
|
||||
: (rnd() < 0.33 ? '-' : (rnd() < 0.66 ? '*' : '+'));
|
||||
const text = sentence(cfg, chain, latinWords, rnd, 4, 10);
|
||||
lines.push(`${marker} ${text}`);
|
||||
}
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
function codeBlock(rnd: () => number): string {
|
||||
const langs = ['ts', 'js', 'json', 'bash', 'md', 'python', 'csharp'];
|
||||
const lang = langs[Math.floor(rnd() * langs.length)];
|
||||
|
||||
const samples: Record<string, string[]> = {
|
||||
ts: [
|
||||
'type User = { id: number; name: string }',
|
||||
'const users: User[] = []',
|
||||
'function addUser(u: User) { users.push(u) }',
|
||||
'addUser({ id: 1, name: "lorem" })',
|
||||
],
|
||||
js: [
|
||||
'const arr = [1,2,3]',
|
||||
'arr.forEach(x => console.log(x))',
|
||||
'function square(n) { return n*n }',
|
||||
'console.log(square(5))',
|
||||
],
|
||||
json: [
|
||||
'{',
|
||||
' "id": 42,',
|
||||
' "name": "ipsum",',
|
||||
' "active": true',
|
||||
'}',
|
||||
],
|
||||
bash: [
|
||||
'#!/bin/bash',
|
||||
'echo "Hello lorem"',
|
||||
'for i in {1..3}; do',
|
||||
' echo "Item $i"',
|
||||
'done',
|
||||
],
|
||||
md: [
|
||||
'# Sample Markdown',
|
||||
'',
|
||||
'- Item one',
|
||||
'- Item two',
|
||||
'',
|
||||
'> Blockquote lorem ipsum',
|
||||
],
|
||||
python: [
|
||||
'def greet(name):',
|
||||
' print(f"Hello {name}")',
|
||||
'',
|
||||
'for n in ["lorem", "ipsum"]:',
|
||||
' greet(n)',
|
||||
],
|
||||
csharp: [
|
||||
'public class Hello {',
|
||||
' public static void Main() {',
|
||||
' Console.WriteLine("Hello lorem");',
|
||||
' }',
|
||||
'}',
|
||||
],
|
||||
};
|
||||
|
||||
const body = samples[lang].join('\n');
|
||||
return `\`\`\`${lang}\n${body}\n\`\`\``;
|
||||
}
|
||||
|
||||
function blockquote(cfg: GeneratorConfig, rnd: () => number, latinWords: string[]): string {
|
||||
const lines = Math.floor(rnd() * 3) + 1;
|
||||
const chain = buildChain(latinWords);
|
||||
return Array.from({ length: lines })
|
||||
.map(() => `> ${sentence(cfg, chain, latinWords, rnd, 6, 12)}`)
|
||||
.join('\n');
|
||||
}
|
||||
|
||||
export function generateMarkdown(cfg: GeneratorConfig): string {
|
||||
const latinWords = getLoremWords(cfg);
|
||||
const rnd = mulberry32(hashSeed(cfg.seedStr));
|
||||
const chain = buildChain(latinWords);
|
||||
const blocks: string[] = [];
|
||||
let currentHeaderLevel = 1;
|
||||
|
||||
for (let i = 0; i < cfg.blocks; i++) {
|
||||
const roll = rnd();
|
||||
if (cfg.enableHeaders && roll < cfg.headerFrequency) {
|
||||
const nextHeader = header(rnd, currentHeaderLevel, cfg.headerStyle, latinWords);
|
||||
blocks.push(nextHeader);
|
||||
// update currentHeaderLevel realistically
|
||||
if (currentHeaderLevel === 1) {
|
||||
currentHeaderLevel = rnd() < 0.7 ? 2 : 1;
|
||||
}
|
||||
else if (currentHeaderLevel === 2) {
|
||||
currentHeaderLevel = rnd() < 0.5 ? 3 : 1;
|
||||
}
|
||||
else {
|
||||
currentHeaderLevel = rnd() < 0.6 ? 2 : 1;
|
||||
}
|
||||
blocks.push(paragraph(cfg, chain, rnd, latinWords));
|
||||
}
|
||||
else if (cfg.enableLists && roll < cfg.headerFrequency + cfg.listFrequency) {
|
||||
blocks.push(listBlock(cfg, rnd, latinWords));
|
||||
}
|
||||
else if (cfg.enableCode && roll < cfg.headerFrequency + cfg.listFrequency + cfg.codeFrequency) {
|
||||
blocks.push(codeBlock(rnd));
|
||||
}
|
||||
else if (cfg.enableBlockquotes
|
||||
&& roll < cfg.headerFrequency + cfg.listFrequency + cfg.codeFrequency + cfg.quoteFrequency) {
|
||||
blocks.push(blockquote(cfg, rnd, latinWords));
|
||||
}
|
||||
else {
|
||||
blocks.push(paragraph(cfg, chain, rnd, latinWords));
|
||||
}
|
||||
}
|
||||
|
||||
if (cfg.inlineLinks && rnd() < 0.3) {
|
||||
blocks.push('[lorem]: https://example.com/lorem');
|
||||
blocks.push('[ipsum]: https://example.com/ipsum');
|
||||
}
|
||||
|
||||
return blocks.join('\n\n');
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
<script setup lang="ts">
|
||||
import { type GeneratorConfig, generateMarkdown, getSupportedLanguages } from './markdown-lorem-ipsum.service';
|
||||
|
||||
const headerStyleOptions = [
|
||||
{ label: 'ATX (#, ##, ###)', value: 'atx' },
|
||||
{ label: 'Setext (====, ----)', value: 'setext' },
|
||||
];
|
||||
const listStyleOptions = [
|
||||
{ label: 'Unordered (-, *, +)', value: 'unordered' },
|
||||
{ label: 'Ordered (1., 2., 3.)', value: 'ordered' },
|
||||
];
|
||||
|
||||
const ui = ref<GeneratorConfig>({
|
||||
seedStr: '',
|
||||
blocks: 12,
|
||||
avgSentencePerPara: 4,
|
||||
enableHeaders: true,
|
||||
enableLists: true,
|
||||
enableCode: true,
|
||||
enableBlockquotes: true,
|
||||
inlineEmphasis: true,
|
||||
inlineStrong: true,
|
||||
inlineLinks: true,
|
||||
inlineCode: true,
|
||||
headerStyle: 'atx',
|
||||
listStyle: 'unordered',
|
||||
headerFrequency: 0.5,
|
||||
listFrequency: 0.05,
|
||||
codeFrequency: 0.15,
|
||||
quoteFrequency: 0.15,
|
||||
language: 'English',
|
||||
});
|
||||
|
||||
const markdown = ref('');
|
||||
|
||||
function generate() {
|
||||
ui.value.seedStr = `seed-${Math.random().toString(36).slice(2)}`;
|
||||
markdown.value = generateMarkdown(ui.value);
|
||||
}
|
||||
|
||||
const supportedLanguages = getSupportedLanguages();
|
||||
|
||||
onMounted(generate);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<n-form :model="ui" label-placement="left">
|
||||
<n-space justify="center">
|
||||
<c-select
|
||||
v-model:value="ui.language"
|
||||
searchable
|
||||
label="Language:"
|
||||
label-position="left"
|
||||
style="width: 200px"
|
||||
:options="Object.values(supportedLanguages)"
|
||||
mb-2
|
||||
/>
|
||||
|
||||
<n-form-item label="Blocks:">
|
||||
<n-input-number v-model:value="ui.blocks" :min="1" :max="50" />
|
||||
</n-form-item>
|
||||
|
||||
<n-form-item label="Sentences per paragraph:">
|
||||
<n-input-number v-model:value="ui.avgSentencePerPara" :min="1" :max="12" />
|
||||
</n-form-item>
|
||||
</n-space>
|
||||
|
||||
<c-card title="Styles" mb-2>
|
||||
<n-space justify="center">
|
||||
<n-form-item label="Block types:">
|
||||
<n-space wrap>
|
||||
<n-checkbox v-model:checked="ui.enableHeaders">
|
||||
Headers
|
||||
</n-checkbox>
|
||||
<n-checkbox v-model:checked="ui.enableLists">
|
||||
Lists
|
||||
</n-checkbox>
|
||||
<n-checkbox v-model:checked="ui.enableCode">
|
||||
Code
|
||||
</n-checkbox>
|
||||
<n-checkbox v-model:checked="ui.enableBlockquotes">
|
||||
Quotes
|
||||
</n-checkbox>
|
||||
</n-space>
|
||||
</n-form-item>
|
||||
|
||||
<n-form-item label="Inline styles">
|
||||
<n-space wrap>
|
||||
<n-checkbox v-model:checked="ui.inlineEmphasis">
|
||||
Emphasis
|
||||
</n-checkbox>
|
||||
<n-checkbox v-model:checked="ui.inlineStrong">
|
||||
Strong
|
||||
</n-checkbox>
|
||||
<n-checkbox v-model:checked="ui.inlineLinks">
|
||||
Links
|
||||
</n-checkbox>
|
||||
<n-checkbox v-model:checked="ui.inlineCode">
|
||||
Inline code
|
||||
</n-checkbox>
|
||||
</n-space>
|
||||
</n-form-item>
|
||||
</n-space>
|
||||
|
||||
<n-space justify="center">
|
||||
<n-form-item label="Header style:">
|
||||
<n-select v-model:value="ui.headerStyle" :options="headerStyleOptions" />
|
||||
</n-form-item>
|
||||
|
||||
<n-form-item label="List style:">
|
||||
<n-select v-model:value="ui.listStyle" :options="listStyleOptions" />
|
||||
</n-form-item>
|
||||
</n-space>
|
||||
</c-card>
|
||||
|
||||
<c-card title="Block frequencies" mb-2>
|
||||
<n-space>
|
||||
<n-form-item label="Headers:">
|
||||
<n-input-number v-model:value="ui.headerFrequency" :step="0.05" :min="0" :max="1" size="small" style="width:180px" />
|
||||
</n-form-item>
|
||||
|
||||
<n-form-item label="Lists:">
|
||||
<n-input-number v-model:value="ui.listFrequency" :step="0.05" :min="0" :max="1" size="small" style="width:180px" />
|
||||
</n-form-item>
|
||||
|
||||
<n-form-item label="Code:">
|
||||
<n-input-number v-model:value="ui.codeFrequency" :step="0.05" :min="0" :max="1" size="small" style="width:180px" />
|
||||
</n-form-item>
|
||||
|
||||
<n-form-item label="Quotes:">
|
||||
<n-input-number v-model:value="ui.quoteFrequency" :step="0.05" :min="0" :max="1" size="small" style="width:180px" />
|
||||
</n-form-item>
|
||||
</n-space>
|
||||
</c-card>
|
||||
|
||||
<n-space justify="center">
|
||||
<n-button type="primary" @click="generate">
|
||||
Generate
|
||||
</n-button>
|
||||
</n-space>
|
||||
</n-form>
|
||||
|
||||
<c-card title="Generated Markdown" mt-2>
|
||||
<textarea-copyable :value="markdown" language="markdown" />
|
||||
</c-card>
|
||||
</div>
|
||||
</template>
|
||||
Reference in new issue
Block a user