Skip to content

Commit

Permalink
feat: pass file to transform option
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Apr 23, 2021
1 parent 7e48127 commit e96ef0e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import { Element, Text } from './types/hast';
const attacher: Attacher = (options) => {
if (!validateOptions(options)) throw new Error('Invalid options');

const transformer: Transformer = async (tree, _file) => {
const transformer: Transformer = async (tree, file) => {
// List of transformations that are ocurring
let transformations: Promise<void>[] = [];

visit<MDASTCode>(tree, 'code', (node) => {
const transform =
typeof options.transform === 'function'
? options.transform(node)
? options.transform(node, file)
: options.transform;
// Asyncronously apply the transformation
transformations.push(
Expand Down
9 changes: 7 additions & 2 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { VFileCompatible } from 'unified';
import * as hast from './types/hast';
import { MDASTCode, MDASTCodeExtra } from './types';

Expand All @@ -13,14 +14,18 @@ export interface TransformResults {
/**
* A general function that applies arbitrary changes to the MDAST node
*/
transform?: (node: MDASTCodeExtra) => void | Promise<void>;
transform?: (
node: MDASTCodeExtra,
file: VFileCompatible
) => void | Promise<void>;
}

export type Options = {
transform:
| TransformResults
| ((
node: MDASTCode
node: MDASTCode,
file: VFileCompatible
) =>
| void
| undefined
Expand Down

0 comments on commit e96ef0e

Please sign in to comment.