Skip to content

Commit

Permalink
fix: unwrap images
Browse files Browse the repository at this point in the history
  • Loading branch information
marbemac committed May 26, 2021
1 parent 03e1c40 commit b712c63
Show file tree
Hide file tree
Showing 14 changed files with 825 additions and 681 deletions.
11 changes: 8 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const esModules = ['unist-util-visit', 'unist-util-select', 'mdast-util-to-string', 'unist-util-is', 'zwitch'].join(
'|',
);
const esModules = [
'unist-util-visit',
'unist-util-select',
'mdast-util-to-string',
'unist-util-is',
'zwitch',
'hast-util-whitespace',
].join('|');

module.exports = {
preset: '@stoplight/scripts',
Expand Down
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@stoplight/types": "^12.3.0",
"@stoplight/yaml": "^4.2.2",
"github-slugger": "^1.3.0",
"hast-util-whitespace": "^2.0.0",
"lodash": "^4.17.21",
"mdast-util-to-string": "^3.1.0",
"remark-frontmatter": "^3.0.0",
Expand All @@ -58,31 +59,31 @@
"unist-util-visit": "^3.1.0"
},
"devDependencies": {
"@babel/core": "^7.14.2",
"@babel/core": "^7.14.3",
"@babel/preset-env": "^7.14.2",
"@babel/preset-typescript": "^7.13.0",
"@rollup/plugin-node-resolve": "^13.0.0",
"@stoplight/eslint-config": "^2.0.0",
"@stoplight/scripts": "^9.0.1",
"@types/hast": "^2.3.1",
"@types/jest": "^26.0.23",
"@types/lodash": "^4.14.169",
"@types/lodash": "^4.14.170",
"@types/unist": "^2.0.3",
"@typescript-eslint/eslint-plugin": "^4.23.0",
"@typescript-eslint/parser": "^4.23.0",
"eslint": "^7.26.0",
"eslint-plugin-import": "^2.22.1",
"@typescript-eslint/eslint-plugin": "^4.25.0",
"@typescript-eslint/parser": "^4.25.0",
"eslint": "^7.27.0",
"eslint-plugin-import": "^2.23.3",
"eslint-plugin-jest": "^24.3.6",
"eslint-plugin-prettier": "^3.4.0",
"eslint-plugin-react": "^7.23.2",
"eslint-plugin-react-hooks": "^4.2.0",
"eslint-plugin-simple-import-sort": "^7.0.0",
"glob-fs": "^0.1.7",
"jest": "^26.6.3",
"jest": "^27.0.1",
"lodash": "^4.17.21",
"prettier": "^2.3.0",
"remark-html": "^13.0.1",
"ts-jest": "^26.5.6",
"ts-jest": "^27.0.0",
"typescript": "^4.2.4"
},
"lint-staged": {
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ newConfig[0].external = [
'@stoplight/yaml',
'lodash',

// mdast-util-to-markdown is CJS, can stay
// these are CJS, can stay
'mdast-util-to-markdown',
];
newConfig[0].plugins.unshift(nodeResolve());
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/getJsonPathForPosition.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('getJsonPathForPosition', () => {
${4} | ${2} | ${['children', 2, 'children', 0, 'children', 0]}
${6} | ${0} | ${['children', 3]}
${7} | ${6} | ${['children', 3]}
${10} | ${8} | ${['children', 4, 'children', 0]}
${10} | ${8} | ${['children', 4]}
${18} | ${3} | ${['children', 8]}
`('should return proper json path for line $line and character $character', ({ line, character, path }) => {
expect(getJsonPathForPosition(result, { line, character })).toEqual(path);
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/getLocationForJsonPath.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('getLocationForJsonPath', () => {
${[4, 0]} | ${[4, 25]} | ${['children', 2]}
${[4, 2]} | ${[4, 25]} | ${['children', 2, 'children', 0, 'children', 0]}
${[6, 0]} | ${[8, 3]} | ${['children', 3]}
${[10, 0]} | ${[10, 29]} | ${['children', 4, 'children', 0]}
${[10, 0]} | ${[10, 29]} | ${['children', 4]}
${[18, 0]} | ${[18, 3]} | ${['children', 8]}
`('should return proper location for given JSONPath $path', ({ start, end, path }) => {
expect(getLocationForJsonPath(result, path)).toEqual({
Expand Down
6 changes: 6 additions & 0 deletions src/ast-types/mdast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ export interface Link extends Parent, Resource {

export interface Image extends Node, Resource, Alternative {
type: 'image';

// custom
annotations?: {
bg?: string;
focus?: 'top' | 'bottom' | 'center' | 'top-right' | 'top-left';
};
}

export interface LinkReference extends Parent, Reference {
Expand Down
2 changes: 2 additions & 0 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
slug,
smdAnnotations,
smdCode,
unwrapImages,
} from './plugins/run';
import { replaceThirdPartyBlocks } from './replaceThirdPartyBlocks';

Expand All @@ -35,6 +36,7 @@ export const remarkParsePreset: unified.Preset<ParseSettings> = {
[remarkFrontmatter, ['yaml']],
remarkGFM,
slug,
unwrapImages,
smdAnnotations,
smdCode,
inlineCodeMdast2Hast,
Expand Down
27 changes: 5 additions & 22 deletions src/plugins/run/__tests__/code.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,12 @@ describe('code plugin', () => {
<p>Starting paragraph.</p>
<codegroup>
<pre>
<code
class=\\"language-bash\\"
lang=\\"bash\\"
meta=\\"title=&#x22;title 1&#x22;\\"
title=\\"title 1\\"
>
<code class=\\"language-bash\\" lang=\\"bash\\" title=\\"title 1\\">
# my bash
</code>
</pre>
<pre>
<code
class=\\"language-js\\"
lang=\\"js\\"
meta=\\"lineNumbers title=&#x22;title 2&#x22;\\"
title=\\"title 2\\"
lineNumbers=\\"true\\"
>
<code class=\\"language-js\\" lang=\\"js\\" title=\\"title 2\\" lineNumbers=\\"true\\">
var x = 'y';
</code>
</pre>
Expand All @@ -65,28 +54,22 @@ describe('code plugin', () => {
<code
class=\\"language-yaml\\"
lang=\\"yaml\\"
meta=\\"title=&#x22;title 1&#x22;\\"
title=\\"title 1\\"
jsonSchema=\\"true\\"
type=\\"json_schema\\"
>
type: object
</code>
</pre>
<p>With legacy json_schema snakecase tag in meta:</p>
<pre>
<code
class=\\"language-json\\"
lang=\\"json\\"
meta=\\"json_schema title=&#x22;title 1&#x22;\\"
title=\\"title 1\\"
jsonSchema=\\"true\\"
>
<code class=\\"language-json\\" lang=\\"json\\" title=\\"title 1\\" jsonSchema=\\"true\\">
type: object
</code>
</pre>
<p>With legacy http annotation:</p>
<pre>
<code class=\\"language-yaml\\" lang=\\"yaml\\" http=\\"true\\">
<code class=\\"language-yaml\\" lang=\\"yaml\\" http=\\"true\\" type=\\"http\\">
method: get
</code>
</pre>
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/run/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ function processNode(node: MDAST.Content, annotations?: object): MDAST.Content {
return {
...node,
annotations,
data: {
...(node.data || {}),
hProperties: annotations,
},
};
}

Expand Down
3 changes: 1 addition & 2 deletions src/plugins/run/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ export const smdCode: unified.Attacher = function () {
const data = node.data || (node.data = {});
data.hProperties = {
lang: node.lang,
meta: node.meta,
...((data.hProperties as any) || {}),
...node.annotations,
...((data.hProperties as any) || {}),
};

/**
Expand Down
1 change: 1 addition & 0 deletions src/plugins/run/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './blockquote';
export * from './code';
export * from './resolver';
export * from './slug';
export * from './unwrap-images';
55 changes: 55 additions & 0 deletions src/plugins/run/unwrap-images.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Pulled over from https://github.com/remarkjs/remark-unwrap-images because hasn't switched to esm yet

import { whitespace } from 'hast-util-whitespace';
import * as unified from 'unified';
import { Parent } from 'unist';
import { SKIP, visit } from 'unist-util-visit';

export const unwrapImages: unified.Attacher = function () {
// Patch slugs on heading nodes.
return function transformer(tree) {
visit<Parent>(tree, 'paragraph', (node, index, parent) => {
if (!index) return;

if (applicable(node)) {
parent?.children.splice(index, 1, ...node.children);
return [SKIP, index];
}

return;
});
};
};

function applicable(node: Parent, inLink?: boolean): boolean | null {
let image: boolean | null = null;
let children = node.children;
let length = children.length;
let index = -1;
let child;
let linkResult;

while (++index < length) {
child = children[index];

if (whitespace(child)) {
// White space is fine.
} else if (child.type === 'image' || child.type === 'imageReference') {
image = true;
} else if (!inLink && (child.type === 'link' || child.type === 'linkReference')) {
linkResult = applicable(child as Parent, true);

if (linkResult === false) {
return false;
}

if (linkResult === true) {
image = true;
}
} else {
return false;
}
}

return image;
}
3 changes: 2 additions & 1 deletion src/plugins/stringify/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ function computeMetaProps(annotations: object) {
}
}

return metaProps;
// remove duplicate values
return [...new Set(metaProps)];
}
Loading

0 comments on commit b712c63

Please sign in to comment.