Skip to content

Commit

Permalink
feat: new build pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
makamekm committed Oct 23, 2024
1 parent 0245e1e commit a1a3034
Show file tree
Hide file tree
Showing 22 changed files with 490 additions and 438 deletions.
496 changes: 185 additions & 311 deletions CHANGELOG.md

Large diffs are not rendered by default.

76 changes: 64 additions & 12 deletions esbuild/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ const common = {
format: 'iife',
plugins,
}),
build({
...common,
entryPoints: ['src/scss/base.scss'],
outfile: 'dist/css/base.css',
format: 'iife',
plugins,
}),
build({
...common,
entryPoints: ['src/scss/_yfm-only.scss'],
outfile: 'dist/css/_yfm-only.css',
format: 'iife',
plugins,
}),
build({
...common,
entryPoints: ['src/scss/print.scss'],
Expand All @@ -60,12 +74,26 @@ const common = {
}),
]);

await build({
...common,
entryPoints: ['dist/css/yfm.css'],
outfile: 'dist/css/yfm.min.css',
minify: true,
});
await Promise.all([
build({
...common,
entryPoints: ['dist/css/yfm.css'],
outfile: 'dist/css/yfm.min.css',
minify: true,
}),
build({
...common,
entryPoints: ['dist/css/base.css'],
outfile: 'dist/css/base.min.css',
minify: true,
}),
build({
...common,
entryPoints: ['dist/css/_yfm-only.css'],
outfile: 'dist/css/_yfm-only.min.css',
minify: true,
}),
]);
})();

(async function buildJs() {
Expand All @@ -75,17 +103,41 @@ const common = {
entryPoints: ['src/js/index.ts'],
outfile: 'dist/js/yfm.js',
}),
build({
...common,
entryPoints: ['src/js/base.ts'],
outfile: 'dist/js/base.js',
}),
build({
...common,
entryPoints: ['src/js/_yfm-only.ts'],
outfile: 'dist/js/_yfm-only.js',
}),
build({
...common,
entryPoints: ['src/js/print/index.ts'],
outfile: 'dist/js/print.js',
}),
]);

await build({
...common,
entryPoints: ['dist/js/yfm.js'],
outfile: 'dist/js/yfm.min.js',
minify: true,
});
await Promise.all([
build({
...common,
entryPoints: ['dist/js/yfm.js'],
outfile: 'dist/js/yfm.min.js',
minify: true,
}),
build({
...common,
entryPoints: ['dist/js/base.js'],
outfile: 'dist/js/base.min.js',
minify: true,
}),
build({
...common,
entryPoints: ['dist/js/_yfm-only.js'],
outfile: 'dist/js/_yfm-only.min.js',
minify: true,
}),
]);
})();
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@diplodoc/transform",
"version": "4.32.4",
"version": "4.34.0",
"description": "A simple transformer of text in YFM (Yandex Flavored Markdown) to HTML",
"keywords": [
"markdown",
Expand Down
1 change: 1 addition & 0 deletions src/js/_yfm-only.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './term';
2 changes: 2 additions & 0 deletions src/js/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import './polyfill';
import './code';
5 changes: 2 additions & 3 deletions src/js/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import '@diplodoc/cut-extension/runtime';
import '@diplodoc/tabs-extension/runtime';

import './polyfill';
import './code';
import './term';
import './base';
import './_yfm-only';
import './wide-mode';
import './patch';
12 changes: 12 additions & 0 deletions src/scss/_yfm-only.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
Note: This file excludes "cut" and "tabs" as they are handled separately
in dedicated extensions (packages). In the future, "note", "file", "term"
and "table" will also be excluded from this file and moved to yfm.scss,
once they are moved to separate packages. Direct usage is not recommended,
as the file is subject to changes without prior notice.
*/

@import 'note';
@import 'file';
@import 'table';
@import 'term';
4 changes: 4 additions & 0 deletions src/scss/base.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@import 'common';
@import 'anchor';
@import 'highlight';
@import 'code';
11 changes: 2 additions & 9 deletions src/scss/yfm.scss
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
@import 'common';
@import 'note';
@import 'anchor';
@import 'highlight';
@import 'code';
@import 'file';
@import 'term';
@import 'table';
@import 'base';
@import 'yfm-only';
@import 'modal';

@import '@diplodoc/cut-extension/runtime';
@import '@diplodoc/tabs-extension/runtime';
37 changes: 37 additions & 0 deletions src/transform/fsContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {readFileSync, writeFileSync} from 'fs';
import {readFile, writeFile} from 'fs/promises';

import {FsContext} from './typings';
import {isFileExists, isFileExistsAsync} from './utilsFS';

export class DefaultFsContext implements FsContext {
exist(path: string): boolean {
return isFileExists(path);
}

read(path: string): string {
return readFileSync(path, 'utf8');
}

write(path: string, content: string): void {
writeFileSync(path, content, {
encoding: 'utf8',
});
}

async existAsync(path: string): Promise<boolean> {
return await isFileExistsAsync(path);
}

async readAsync(path: string): Promise<string> {
return readFile(path, 'utf8');
}

async writeAsync(path: string, content: string): Promise<void> {
writeFile(path, content, {
encoding: 'utf8',
});
}
}

export const defaultFsContext = new DefaultFsContext();
19 changes: 10 additions & 9 deletions src/transform/plugins/images/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ type Options = MarkdownItPluginOpts & {
singlePage: boolean;
};

const collect = (input: string, options: Options) => {
const collect = async (input: string, options: Options) => {
const md = new MarkdownIt().use(imsize);

const {root, path, destPath = '', copyFile, singlePage} = options;
const {root, path, destPath = '', copyFile, singlePage, deps} = options;
const tokens = md.parse(input, {});
let result = input;

tokens.forEach((token) => {
for (const token of tokens) {
if (token.type !== 'inline') {
return;
}

const children = token.children || [];

children.forEach((childToken) => {
for (const childToken of children) {
if (childToken.type !== 'image') {
return;
}
Expand All @@ -40,15 +40,16 @@ const collect = (input: string, options: Options) => {
const targetPath = resolveRelativePath(path, src);
const targetDestPath = resolveRelativePath(destPath, src);

if (singlePage && !path.includes('_includes/')) {
const newSrc = relative(root, resolveRelativePath(path, src));
deps?.markDep?.(path, targetPath, 'image');

if (singlePage && !path.includes('_includes/')) {
const newSrc = relative(root, targetPath);
result = result.replace(src, newSrc);
}

copyFile(targetPath, targetDestPath);
});
});
await copyFile(targetPath, targetDestPath);
}
}

if (singlePage) {
return result;
Expand Down
26 changes: 17 additions & 9 deletions src/transform/plugins/images/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ import {join, sep} from 'path';
import {bold} from 'chalk';
import {optimize} from 'svgo';
import Token from 'markdown-it/lib/token';
import {readFileSync} from 'fs';

import {isFileExists, resolveRelativePath} from '../../utilsFS';
import {resolveRelativePath} from '../../utilsFS';
import {isExternalHref, isLocalUrl} from '../../utils';
import {MarkdownItPluginCb, MarkdownItPluginOpts} from '../typings';
import {StateCore} from '../../typings';
import {FsContext, StateCore} from '../../typings';
import {defaultFsContext} from '../../fsContext';

interface ImageOpts extends MarkdownItPluginOpts {
assetsPublicPath: string;
inlineSvg?: boolean;
}

function replaceImageSrc(
fs: FsContext,
token: Token,
state: StateCore,
{assetsPublicPath = sep, root = '', path: optsPath, log}: ImageOpts,
{assetsPublicPath = sep, root = '', path: optsPath, log, deps}: ImageOpts,
) {
const src = token.attrGet('src') || '';
const currentPath = state.env.path || optsPath;
Expand All @@ -28,7 +29,9 @@ function replaceImageSrc(

const path = resolveRelativePath(currentPath, src);

if (isFileExists(path)) {
deps?.markDep?.(currentPath, path, 'image');

if (fs.exist(path)) {
state.md.assets?.push(path);
} else {
log.error(`Asset not found: ${bold(src)} in ${bold(currentPath)}`);
Expand All @@ -51,15 +54,18 @@ function prefix() {
}

function convertSvg(
fs: FsContext,
token: Token,
state: StateCore,
{path: optsPath, log, notFoundCb, root}: SVGOpts,
{path: optsPath, log, notFoundCb, root, deps}: SVGOpts,
) {
const currentPath = state.env.path || optsPath;
const path = resolveRelativePath(currentPath, token.attrGet('src') || '');

try {
const raw = readFileSync(path).toString();
deps?.markDep?.(currentPath, path, 'image');

const raw = fs.read(path).toString();
const result = optimize(raw, {
plugins: [
{
Expand Down Expand Up @@ -90,6 +96,8 @@ function convertSvg(
type Opts = SVGOpts & ImageOpts;

const index: MarkdownItPluginCb<Opts> = (md, opts) => {
const fs = opts.fs ?? defaultFsContext;

md.assets = [];

const plugin = (state: StateCore) => {
Expand Down Expand Up @@ -117,9 +125,9 @@ const index: MarkdownItPluginCb<Opts> = (md, opts) => {
const shouldInlineSvg = opts.inlineSvg !== false && !isExternalHref(imgSrc);

if (imgSrc.endsWith('.svg') && shouldInlineSvg) {
childrenTokens[j] = convertSvg(childrenTokens[j], state, opts);
childrenTokens[j] = convertSvg(fs, childrenTokens[j], state, opts);
} else {
replaceImageSrc(childrenTokens[j], state, opts);
replaceImageSrc(fs, childrenTokens[j], state, opts);
}

childrenTokens[j].attrSet('yfm_patched', '1');
Expand Down
Loading

0 comments on commit a1a3034

Please sign in to comment.