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

If the image is followed by italics, it should be converted without spaces. #353

Open
4 tasks done
songkg7 opened this issue Mar 3, 2024 · 1 comment
Open
4 tasks done
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@songkg7
Copy link
Owner

songkg7 commented Mar 3, 2024

Describe the bug

If the image is followed by italics, it should be converted without empty line. also, Add the word image in empty square brackets.

To Reproduce

o2 conversion contains image link and italics

![](https://image.link)

_description_

Expected behavior

![image](https://image.link)
_description_

Guide

  • add test code in tests/ResourceLinkConverter.test.ts
  • need to modify src/jekyll/ResourceLinkConverter.ts
Checklist
  • Modify src/jekyll/ResourceLinkConverter.ts45b5f9e Edit
  • Running GitHub Actions for src/jekyll/ResourceLinkConverter.tsEdit
  • Modify src/tests/ResourceLinkConverter.test.ts13441c0 Edit
  • Running GitHub Actions for src/tests/ResourceLinkConverter.test.tsEdit
@songkg7 songkg7 added bug Something isn't working sweep Assigns Sweep to an issue or pull request. labels Mar 3, 2024
Copy link
Contributor

sweep-ai bot commented Mar 3, 2024

🚀 Here's the PR! #356

See Sweep's progress at the progress dashboard!
Sweep Basic Tier: I'm using GPT-4. You have 4 GPT-4 tickets left for the month and 2 for the day. (tracking ID: b8214aed58)

For more GPT-4 tickets, visit our payment portal. For a one week free trial, try Sweep Pro (unlimited GPT-4 tickets).

Tip

I'll email you at [email protected] when I complete this pull request!


Actions (click)

  • ↻ Restart Sweep

GitHub Actions✓

Here are the GitHub Actions logs prior to making any changes:

Sandbox logs for 46ac56c
Checking src/jekyll/ResourceLinkConverter.ts for syntax errors... ✅ src/jekyll/ResourceLinkConverter.ts has no syntax errors! 1/1 ✓
Checking src/jekyll/ResourceLinkConverter.ts for syntax errors...
✅ src/jekyll/ResourceLinkConverter.ts has no syntax errors!

Sandbox passed on the latest main, so sandbox checks will be enabled for this issue.


Step 1: 🔎 Searching

I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.

Some code snippets I think are relevant in decreasing order of relevance (click to expand). If some file is missing from here, you can mention the path in the ticket description.

import { extractResourceNames, ResourceLinkConverter } from '../jekyll/ResourceLinkConverter';
jest.mock('obsidian', () => ({}), { virtual: true });
jest.mock('fs', () => ({
mkdirSync: jest.fn(),
copyFile: jest.fn(),
}));
describe('extract image name', () => {
it('should return image name array', () => {
const context = `![[test.png]]
test
![[image.png]]
`;
const result = extractResourceNames(context);
expect(result).toEqual(['test.png', 'image.png']);
});
it('should return undefined', () => {
const context = `test`;
const result = extractResourceNames(context);
expect(result).toBeUndefined();
});
});
describe('convert called', () => {
const converter = new ResourceLinkConverter(
'2023-01-01-post-mock',
'assets',
'test',
'attachments',
'assets',
);
it('should return converted post', () => {
expect(converter.convert(`![[test.png]]`)).toEqual(`![image](/assets/2023-01-01-post-mock/test.png)`);
});
});
describe('resize image', () => {
const converter = new ResourceLinkConverter(
'2023-01-01-post-mock',
'assets',
'test',
'attachments',
'assets',
);
it('should return converted attachments with width', () => {
expect(converter.convert(`![[test.png|100]]`)).toEqual(`![image](/assets/2023-01-01-post-mock/test.png){: width="100" }`);
});
it('should return converted attachments with width and height', () => {
expect(converter.convert(`![[test.png|100x200]]`))
.toEqual(`![image](/assets/2023-01-01-post-mock/test.png){: width="100" height="200" }`);
});
it('should ignore size when image resize syntax was invalid', () => {
expect(converter.convert(`![[test.png|x100]]`))
.toEqual(`![image](/assets/2023-01-01-post-mock/test.png)`);
});
});
describe('image caption', () => {
const converter = new ResourceLinkConverter(
'2023-01-01-post-mock',
'assets',
'test',
'attachments',
'assets',
);
it('should remove blank line after attachments', () => {
const context = `
![[test.png]]
_This is a test image._
`;
const result = converter.convert(context);
expect(result).toEqual(`
![image](/assets/2023-01-01-post-mock/test.png)
_This is a test image._
`);
});
it('should insert next line if no more space after attachment', () => {
const context = `
![[test.png]]_This is a test image._
`;
const result = converter.convert(context);
expect(result).toEqual(`
![image](/assets/2023-01-01-post-mock/test.png)
_This is a test image._
`);
});
it('should nothing if exist just one blank line', () => {
const context = `
![[test.png]]
_This is a test image._
`;
const result = converter.convert(context);
expect(result).toEqual(`
![image](/assets/2023-01-01-post-mock/test.png)
_This is a test image._
`);
});
it('should nothing if does not exist image caption', () => {
const context = `
![[test.png]]
## Header
`;
const result = converter.convert(context);
expect(result).toEqual(`
![image](/assets/2023-01-01-post-mock/test.png)
## Header
`);
});

import fs from 'fs';
import { ObsidianRegex } from '../ObsidianRegex';
import { Notice } from 'obsidian';
import { Converter } from '../core/Converter';
export class ResourceLinkConverter implements Converter {
private readonly fileName: string;
private readonly resourcePath: string;
private readonly absolutePath: string;
private readonly attachmentsFolder: string;
private readonly relativeResourcePath: string;
constructor(fileName: string, resourcePath: string, absolutePath: string, attachmentsFolder: string, relativeResourcePath: string) {
this.fileName = fileName;
this.resourcePath = resourcePath;
this.absolutePath = absolutePath;
this.attachmentsFolder = attachmentsFolder;
this.relativeResourcePath = relativeResourcePath;
}
convert(input: string): string {
const resourcePath = `${this.resourcePath}/${this.fileName}`;
const resourceNames = extractResourceNames(input);
if (!(resourceNames === undefined || resourceNames.length === 0)) {
fs.mkdirSync(resourcePath, { recursive: true });
}
resourceNames?.forEach((resourceName) => {
fs.copyFile(
`${this.absolutePath}/${this.attachmentsFolder}/${resourceName}`,
`${resourcePath}/${(resourceName.replace(/\s/g, '-'))}`,
(err) => {
if (err) {
// ignore error
console.error(err);
new Notice(err.message);
}
},
);
});
const replacer = (match: string,
contents: string,
suffix: string,
width: string | undefined,
height: string | undefined,
space: string | undefined,
caption: string | undefined) =>
`![image](/${this.relativeResourcePath}/${this.fileName}/${contents.replace(/\s/g, '-')}.${suffix})`
+ `${convertImageSize(width, height)}`
+ `${convertImageCaption(caption)}`;
return input.replace(ObsidianRegex.ATTACHMENT_LINK, replacer);
}
}
export function extractResourceNames(content: string) {
const result = content.match(ObsidianRegex.ATTACHMENT_LINK);
if (result === null) {
return undefined;
}
return result.map((imageLink) => imageLink.replace(ObsidianRegex.ATTACHMENT_LINK, '$1.$2'));
}
function convertImageSize(width: string | undefined, height: string | undefined) {
if (width === undefined || width.length === 0) {
return '';
}
if (height === undefined || height.length === 0) {
return `{: width="${width}" }`;
}
return `{: width="${width}" height="${height}" }`;
}
function convertImageCaption(caption: string | undefined) {
if (caption === undefined || caption.length === 0) {
return '';
}

I also found the following external resources that might be helpful:

Summaries of links found in the content:

https://image.link:

The page describes a bug where an image followed by italics should be converted without an empty line. The expected behavior is to have the image link enclosed in square brackets with the word "image" and followed by the italics. The page provides a guide on how to fix the bug by adding test code in tests/ResourceLinkConverter.test.ts and modifying src/jekyll/ResourceLinkConverter.ts.


Step 2: ⌨️ Coding

  • Modify src/jekyll/ResourceLinkConverter.ts45b5f9e Edit
Modify src/jekyll/ResourceLinkConverter.ts with contents:
• Inside the `convert` method, adjust the `replacer` function to check if the `caption` parameter is not undefined and starts with an underscore (indicating italics). If so, ensure the converted markdown does not include an empty line between the image and the caption. This involves modifying the return statement of the `replacer` function to conditionally format the output based on the presence and format of the caption.
• Ensure the word "image" is included within the square brackets in the converted markdown. This can be achieved by modifying the markdown string in the `replacer` function to `![image](/${this.relativeResourcePath}/${this.fileName}/${contents.replace(/\s/g, '-')}.${suffix})`.
--- 
+++ 
@@ -48,7 +48,7 @@
                       caption: string | undefined) =>
       `![image](/${this.relativeResourcePath}/${this.fileName}/${contents.replace(/\s/g, '-')}.${suffix})`
       + `${convertImageSize(width, height)}`
-      + `${convertImageCaption(caption)}`;
+      + `${caption !== undefined && caption.startsWith('_') ? caption : '\n' + caption}`;
 
     return input.replace(ObsidianRegex.ATTACHMENT_LINK, replacer);
   }
  • Running GitHub Actions for src/jekyll/ResourceLinkConverter.tsEdit
Check src/jekyll/ResourceLinkConverter.ts with contents:

Ran GitHub Actions for 45b5f9e73b90851337b753611f35017ff0c356ac:
• Lint Code Base:

  • Modify src/tests/ResourceLinkConverter.test.ts13441c0 Edit
Modify src/tests/ResourceLinkConverter.test.ts with contents:
• Add a new test case under the `describe` block for image caption conversion to test the specific scenario described in the issue. The test should verify that an image followed by italics is converted correctly, without an empty line between them, and includes the word "image" in the square brackets.
• The test case should use a string that includes an image link followed by italics, similar to the example provided in the issue description. It should assert that the output matches the expected markdown format.
• This test ensures that the conversion logic correctly handles images followed by italics, adhering to the expected behavior.
--- 
+++ 
@@ -110,6 +110,11 @@
     const result = converter.convert(context);
     expect(result).toEqual(`
 ![image](/assets/2023-01-01-post-mock/test.png)
+it('should convert image followed by italics without an empty line and include "image" in square brackets', () => {
+  const context = `![](https://image.link)_description_`;
+  const result = converter.convert(context);
+  expect(result).toEqual(`![image](https://image.link)_description_`);
+});
 _This is a test image._
 `);
   });
  • Running GitHub Actions for src/tests/ResourceLinkConverter.test.tsEdit
Check src/tests/ResourceLinkConverter.test.ts with contents:

Ran GitHub Actions for 13441c099d8feca7e20666b9232836c44d699f68:
• Lint Code Base:


Step 3: 🔁 Code Review

I have finished reviewing the code for completeness. I did not find errors for sweep/if_the_image_is_followed_by_italics_it_s.


🎉 Latest improvements to Sweep:
  • New dashboard launched for real-time tracking of Sweep issues, covering all stages from search to coding.
  • Integration of OpenAI's latest Assistant API for more efficient and reliable code planning and editing, improving speed by 3x.
  • Use the GitHub issues extension for creating Sweep issues directly from your editor.

💡 To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request.Something wrong? Let us know.

This is an automated message generated by Sweep AI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment