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

Fix plugin live reloading for relative file paths #974

Merged
merged 1 commit into from
Jan 16, 2020

Conversation

ang-zeyu
Copy link
Contributor

What is the purpose of this pull request? (put "X" next to an item, remove the rest)

• [x] Documentation update
• [x] Bug fix
• [x] Enhancement to an existing feature

Fixes #913

What is the rationale for this request?
As mentioned in #913, relative file paths for sources defined by plugins are not correctly resolved
against the page being processed. If the source is also part of an included file, then it should be resolved against the included file.

Urls are also added to the page's pluginSourceFiles, which has no effect, but is incorrect

What changes did you make? (Give an overview)
Let's fix this and simplify the api for defining sources.

getSources now returns

{
  tagMap: [ ['tag name', 'src attribute name' ], ['tag name', 'src attribute name' ], ... ]
  sources: as before, which is an array of absolute / relative file paths to watch
}

From a plugin author's perspective, resolving said sources against the page being processed
/ the included file is tricky. This moves all such logic to Page.js, exposing a simpler api.

The old return syntax is deprecated, but still supported as well.

Provide some example code that this change will affect:

getSources: () => ({
  tagMap: [['puml', 'src']],
}),

Testing instructions:
Simply use npm run test to run the new unit tests,

For manual testing of live reloading, the test_site has some
sample pumls at the bottom.

Proposed commit message: (wrap lines at 72 characters)
Fix plugin live reloading for relative file paths

Relative file paths provided by plugins are not resolved against the
processed page or included file.

This results in live reloading failing for pages not in the root
directory.

Let's fix this, and also rewrite plugin source collection, allowing
plugins to simply define tags and corresponding source attributes
to watch.

Copy link
Member

@yamgent yamgent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried it out and seems to be working, nice job. 👍

const Page = require('../../src/Page');


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the extra blank line

- **Parameters:**
- `content`: The raw Markdown of the current Markdown file (`.md`, `.mbd`, etc.).
- `pluginContext`: User provided parameters for the plugin. This can be specified in the `site.json`.
- `frontMatter`: The frontMatter of the page being processed, in case any frontMatter data is required.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for deviating from the original ordering that the others follow (i.e. parameters, then what it returns)? Seems like the existing format can still be used here, even if it has more information compared to the rest like getLinks() and getScripts().

path.resolve('/root/subdir/images/sample2.png'))).toEqual(true);
expect(page.pluginSourceFiles.has(
path.resolve('/root/images/sample3.png'))).toEqual(true);
expect(page.pluginSourceFiles.has(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems that the pluginSourceFiles variable should just be a Set.

test/unit/utils/pageData.js Show resolved Hide resolved
@ang-zeyu
Copy link
Contributor Author

Updated, thanks for looking through it!

Copy link
Member

@yamgent yamgent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, now just nits in documentations.

docs/userGuide/usingPlugins.md Show resolved Hide resolved
- For relative file paths, _if the tag is part of some included content_ ( eg. `<include />` tags ), it will be resolved against the included page.
Otherwise, it is resolved against the page being processed.
- `sources`: An array of source file paths to watch, where relative file paths are resolved only against the page being processed.
- You can also directly return an array of source file paths to watch ( ie. the `sources` field ) ___(deprecated)___
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact the use case here and the one above seems to be the same, do we need this line in that case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I actually meant only to deprecate the old syntax, not the functionality of returning some manually specified file paths (where relative ones are resolved according to the file processed).

This can be useful if a plugin needs live reload for a file that isn't specified as some src attribute in the dom. E.g. a plugin where you can have file_name+_page_extension that adds some dynamic content to the relavant page based on that file's contents.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm ok, the sentence standalone feels a bit difficult to comprehend without your additional explanation (because of the (ie. the sources field) portion), but I am fine with leaving it as it is for the moment.

- `pluginContext`: User provided parameters for the plugin. This can be specified in the `site.json`.
- `frontMatter`: The frontMatter of the page being processed, in case any frontMatter data is required.

`getSources(content, pluginContext, frontMatter)`: called _before_ a Markdown file's `preRender` function is called.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit: called -> Called

- `tagMap`: An array consisting of `['tag name', 'source attribute name']` key value pairs.
- MarkBind will automatically search for matching tags with the source attributes, and watch them.
- For relative file paths, _if the tag is part of some included content_ ( eg. `<include />` tags ), it will be resolved against the included page.
Otherwise, it is resolved against the page being processed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit: join this line with the previous line?

- For relative file paths, _if the tag is part of some included content_ ( eg. `<include />` tags ), it will be resolved against the included page.
Otherwise, it is resolved against the page being processed.
- `sources`: An array of source file paths to watch, where relative file paths are resolved only against the page being processed.
- You can also directly return an array of source file paths to watch ( ie. the `sources` field ) ___(deprecated)___

Example usage of `getSources` from the PlantUML plugin:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that the reader might have a bit of difficulty understanding the example without context. Maybe an addition like this will make it clearer?

"Example usage of getSources from the PlantUML plugin, which will watch files from tags such as <puml src="..." />:"

Relative file paths provided by plugins are not resolved against the
processed page or included file.

This results in live reloading failing for pages not in the root
directory.

Let's fix this, and also rewrite plugin source collection, allowing
plugins to simply define tags and corresponding source attributes
to watch.

This moves the logic for checking whether the source is part of an
included file to the main markbind repo, reducing potential code
duplication and simplifying the api.
@ang-zeyu
Copy link
Contributor Author

Updated, save for the deprecated syntax parts

@yamgent yamgent added this to the v2.9.1 milestone Jan 15, 2020
@yamgent yamgent merged commit 0acd97c into MarkBind:master Jan 16, 2020
@le0tan le0tan mentioned this pull request Jan 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support live-preview for .puml files
2 participants