Skip to content

Commit

Permalink
Merge pull request #33 from zakhenry/feat/prioritise-comment-block
Browse files Browse the repository at this point in the history
Feat/prioritise comment block
  • Loading branch information
zakhenry authored Jul 13, 2019
2 parents acccb4d + 7a2ac3b commit a5421f3
Show file tree
Hide file tree
Showing 4 changed files with 349 additions and 41 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build": "tsc",
"write-help-output": "echo \"$ embedme --help\" > readme/help-output.txt && node dist/embedme.js --help >> readme/help-output.txt",
"start": "node dist/embedme.js README.md",
"test:write": "node dist/embedme.js test/fixture.md && yarn prettier:write",
"test:write": "node dist/embedme.js test/fixture-source.md --stdout > test/fixture.md && node dist/embedme.js test/fixture.md --check && yarn prettier:write",
"test:check": "! git status | grep test/fixture.md || (echo 'You must commit build and commit changes to test/fixture.md!' && exit 1)",
"test": "yarn test:write && yarn test:check",
"commit": "git add . && git-cz",
Expand Down
83 changes: 44 additions & 39 deletions src/embedme.lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ const filetypeCommentReaders: Record<CommentFamily, FilenameFromCommentReader> =
return match[1];
},
[CommentFamily.HASH]: leadingSymbol('#'),
[CommentFamily.SINGLE_QUOTE]: leadingSymbol('//'),
[CommentFamily.SINGLE_QUOTE]: leadingSymbol(`'`),
[CommentFamily.DOUBLE_PERCENT]: leadingSymbol('%%'),
};

Expand Down Expand Up @@ -189,50 +189,55 @@ function getReplacement(
logMethod(logPrefix, ...messages);
};

if (!codeExtension) {
log({ returnSnippet: substr }, chalk.blue(`No code extension detected, skipping code block...`));
return substr;
}

if (!firstLine && !commentEmbedOverrideFilepath) {
log({ returnSnippet: substr }, chalk.blue(`Code block is empty & no preceding embedme comment, skipping...`));
return substr;
}

if (ignoreNext) {
log({ returnSnippet: substr }, chalk.blue(`"Ignore next" comment detected, skipping code block...`));
return substr;
}

const supportedFileTypes: SupportedFileType[] = Object.values(SupportedFileType).filter(x => typeof x === 'string');
let commentedFilename: string | null;
if (commentEmbedOverrideFilepath) {
commentedFilename = commentEmbedOverrideFilepath;
} else {
if (!codeExtension) {
log({ returnSnippet: substr }, chalk.blue(`No code extension detected, skipping code block...`));
return substr;
}

if (supportedFileTypes.indexOf(codeExtension) < 0) {
log(
{ returnSnippet: substr },
chalk.yellow(
`Unsupported file extension [${codeExtension}], supported extensions are ${supportedFileTypes.join(
', ',
)}, skipping code block`,
),
);
return substr;
}
if (!firstLine) {
log({ returnSnippet: substr }, chalk.blue(`Code block is empty & no preceding embedme comment, skipping...`));
return substr;
}

const languageFamily: CommentFamily | null = lookupLanguageCommentFamily(codeExtension);
const supportedFileTypes: SupportedFileType[] = Object.values(SupportedFileType).filter(x => typeof x === 'string');

if (supportedFileTypes.indexOf(codeExtension) < 0) {
log(
{ returnSnippet: substr },
chalk.yellow(
`Unsupported file extension [${codeExtension}], supported extensions are ${supportedFileTypes.join(
', ',
)}, skipping code block`,
),
);
return substr;
}

if (languageFamily == null) {
log(
{ returnSnippet: substr },
chalk.red(
`File extension ${chalk.underline(
codeExtension,
)} marked as supported, but comment family could not be determined. Please report this issue.`,
),
);
return substr;
}
const languageFamily: CommentFamily | null = lookupLanguageCommentFamily(codeExtension);

if (languageFamily == null) {
log(
{ returnSnippet: substr },
chalk.red(
`File extension ${chalk.underline(
codeExtension,
)} marked as supported, but comment family could not be determined. Please report this issue.`,
),
);
return substr;
}

const commentedFilename = commentEmbedOverrideFilepath || filetypeCommentReaders[languageFamily](firstLine);
commentedFilename = filetypeCommentReaders[languageFamily](firstLine);
}

if (!commentedFilename) {
log(
Expand All @@ -245,7 +250,7 @@ function getReplacement(
const matches = commentedFilename.match(/\s?(\S+?)((#L(\d+)-L(\d+))|$)/m);

if (!matches) {
log({ returnSnippet: substr }, chalk.gray(`No file found in first comment block`));
log({ returnSnippet: substr }, chalk.gray(`No file found in embed line`));
return substr;
}

Expand Down Expand Up @@ -341,7 +346,7 @@ function getReplacement(
return substr;
}

const chalkColour = options.verify ? 'red' : 'green';
const chalkColour = options.verify ? 'yellow' : 'green';

log(
{ returnSnippet: replacement },
Expand Down Expand Up @@ -378,7 +383,7 @@ export function embedme(sourceText: string, inputFilePath: string, options: Embe
const [codeFence, leadingSpaces] = result;
const start = sourceText.substring(previousEnd, result.index);

const extensionMatch = codeFence.match(/```(\S*)/);
const extensionMatch = codeFence.match(/```(.*)/);

const codeExtension = extensionMatch ? extensionMatch[1] : null;
const splitFence = codeFence.split('\n');
Expand Down
Loading

0 comments on commit a5421f3

Please sign in to comment.