Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(file): replaced own plugin with file-extension #572

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
},
"dependencies": {
"@diplodoc/cut-extension": "^0.3.0",
"@diplodoc/file-extension": "^0.1.0",
"@diplodoc/tabs-extension": "^3.5.0",
"chalk": "^4.1.2",
"cheerio": "^1.0.0",
Expand Down
33 changes: 0 additions & 33 deletions src/scss/_file.scss

This file was deleted.

5 changes: 2 additions & 3 deletions src/scss/_yfm-only.scss
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/**
Note: This file excludes "cut" and "tabs" as they are handled separately
in dedicated extensions (packages). In the future, "note", "file", "term"
Note: This file excludes "cut", "file" and "tabs" as they are handled separately
in dedicated extensions (packages). In the future, "note", "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';
1 change: 1 addition & 0 deletions src/scss/yfm.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
@import 'yfm-only';
@import 'modal';
@import '@diplodoc/cut-extension/runtime';
@import '@diplodoc/file-extension/runtime';
@import '@diplodoc/tabs-extension/runtime';
34 changes: 0 additions & 34 deletions src/transform/plugins/file/README.md

This file was deleted.

58 changes: 13 additions & 45 deletions src/transform/plugins/file/const.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,14 @@
export enum FileSpecialAttr {
Src = 'src',
Name = 'name',
Lang = 'lang',
}

// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attributes
export enum LinkHtmlAttr {
Download = 'download',
Href = 'href',
HrefLang = 'hreflang',
Media = 'media',
Ping = 'ping',
ReferrerPolicy = 'referrerpolicy',
Rel = 'rel',
Target = 'target',
Type = 'type',
}

export const FILE_TO_LINK_ATTRS_MAP: Record<FileSpecialAttr, LinkHtmlAttr> = {
[FileSpecialAttr.Src]: LinkHtmlAttr.Href,
[FileSpecialAttr.Name]: LinkHtmlAttr.Download,
[FileSpecialAttr.Lang]: LinkHtmlAttr.HrefLang,
};

export const RULE_NAME = 'yfm_file_inline';
export const KNOWN_ATTRS: readonly string[] = [
FileSpecialAttr.Src,
FileSpecialAttr.Name,
FileSpecialAttr.Lang,
LinkHtmlAttr.ReferrerPolicy,
LinkHtmlAttr.Rel,
LinkHtmlAttr.Target,
LinkHtmlAttr.Type,
];
export const REQUIRED_ATTRS: readonly string[] = [FileSpecialAttr.Src, FileSpecialAttr.Name];

export const FILE_TOKEN = 'yfm_file';

export const PREFIX = '{% file ';
import {FILE_MARKUP_PREFIX} from '@diplodoc/file-extension';
export {
FileSpecialAttr,
FILE_TO_LINK_ATTRS_MAP,
FILE_REQUIRED_ATTRS as REQUIRED_ATTRS,
FILE_TOKEN,
FILE_KNOWN_ATTRS as KNOWN_ATTRS,
FileClassName,
FILE_RULE_NAME as RULE_NAME,
FileHtmlAttr as LinkHtmlAttr,
} from '@diplodoc/file-extension';

export const PREFIX = FILE_MARKUP_PREFIX;
export const PREFIX_LENGTH = PREFIX.length;

export enum FileClassName {
Link = 'yfm-file',
Icon = 'yfm-file__icon',
}
83 changes: 1 addition & 82 deletions src/transform/plugins/file/file.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1 @@
import type MarkdownIt from 'markdown-it';
import type ParserInline from 'markdown-it/lib/parser_inline';
import type Renderer from 'markdown-it/lib/renderer';

import {
FILE_TOKEN,
FILE_TO_LINK_ATTRS_MAP,
FileClassName,
FileSpecialAttr,
KNOWN_ATTRS,
PREFIX,
PREFIX_LENGTH,
REQUIRED_ATTRS,
} from './const';

export type FileOptions = {
fileExtraAttrs: [string, string][];
};

export const fileRenderer = (md: MarkdownIt): Renderer.RenderRule => {
const iconHtml = `<span class="${md.utils.escapeHtml(FileClassName.Icon)}"></span>`;
return (tokens, idx, _opts, _env, self) => {
const token = tokens[idx];
return `<a${self.renderAttrs(token)}>${iconHtml}${md.utils.escapeHtml(token.content)}</a>`;
};
};

export const fileParser = (_md: MarkdownIt, opts?: FileOptions): ParserInline.RuleInline => {
return (state, silent) => {
if (state.src.substring(state.pos, state.pos + PREFIX_LENGTH) !== PREFIX) return false;

// the rest of line after '{% file '
const searchStr = state.src.slice(state.pos + PREFIX_LENGTH, state.posMax);
// loking for pattern 'src="..." name="..." etc="value" %}'
const matchResult = searchStr.match(/^((?:\s*\w+=(?:"[^"]+"|'[^']+')\s)+)\s*%}/);
if (!matchResult) return false;

const paramsGroupLength = matchResult[0].length; // '(src="..." name="...")'.length
const paramsStr = matchResult[1]; // 'src="..." name="..."'

// find pairs of key="foo" or key='bar'
const params = paramsStr.match(/\w+=(?:"[^"]+"|'[^']+')/g);
if (!params) return false;

const attrsObj: Record<string, string> = {};
params.forEach((param) => {
const indexKey = param.indexOf('=');
const key = param.slice(0, indexKey);
const value = param.slice(indexKey + 2, -1);
if (KNOWN_ATTRS.includes(key) && value) {
attrsObj[key] = value;
}
});

const hasAllRequiredAttrs = REQUIRED_ATTRS.every((attr) => attr in attrsObj);
if (!hasAllRequiredAttrs) return false;

if (!silent) {
const token = state.push(FILE_TOKEN, '', 0);
token.block = false;
token.markup = PREFIX;
token.content = attrsObj[FileSpecialAttr.Name];
token.attrs = Object.entries(attrsObj);
token.attrSet('class', FileClassName.Link);

for (const attr of token.attrs) {
if (attr[0] in FILE_TO_LINK_ATTRS_MAP) {
attr[0] = FILE_TO_LINK_ATTRS_MAP[attr[0] as FileSpecialAttr];
}
}

if (Array.isArray(opts?.fileExtraAttrs)) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
token.attrs.push(...opts!.fileExtraAttrs);
}
}

state.pos = state.pos + PREFIX_LENGTH + paramsGroupLength;

return true;
};
};
export type {PluginOptions as FileOptions} from '@diplodoc/file-extension';
12 changes: 2 additions & 10 deletions src/transform/plugins/file/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
import type {PluginWithOptions} from 'markdown-it';
import {transform} from '@diplodoc/file-extension';

import {FILE_TOKEN, RULE_NAME} from './const';
import {FileOptions, fileParser, fileRenderer} from './file';

const filePlugin: PluginWithOptions<FileOptions> = (md, opts) => {
md.inline.ruler.push(RULE_NAME, fileParser(md, opts));
md.renderer.rules[FILE_TOKEN] = fileRenderer(md);
};

export = filePlugin;
export = transform({bundle: false});
Loading