Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
### Added
- Create service class for downloading and extracting JSON
[#553](https://github.com/nextcloud/cookbook/pull/553) @christianlupus
- Show recipe titles for internal references
[#1063](https://github.com/nextcloud/cookbook/pull/1063) @christianlupus

### Changed
- Extracted user folder handling into its own helper class
Expand Down
5 changes: 4 additions & 1 deletion src/components/RecipeIngredient.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
@click="toggleDone"
>
<div class="checkmark" :class="{ done: isDone }">✔</div>
<!-- eslint-disable-next-line vue/no-v-html -->
<div class="ingredient">
<VueShowdown :markdown="displayIngredient" />
</div>
Expand Down Expand Up @@ -92,4 +91,8 @@ li > .ingredient {
margin-left: 0.3em;
text-indent: -1em;
}

.ingredient >>> a {
text-decoration: underline;
}
</style>
1 change: 0 additions & 1 deletion src/components/RecipeInstruction.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<template>
<!-- eslint-disable-next-line vue/no-v-html -->
<li :class="{ done: isDone }" @click="toggleDone">
<VueShowdown :markdown="instruction" class="markdown-instruction" />
</li>
Expand Down
9 changes: 7 additions & 2 deletions src/components/RecipeTool.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<!-- eslint-disable-next-line vue/no-v-html -->
<li v-html="tool"></li>
<li>
<VueShowdown :markdown="tool" class="markdown-tool" />
</li>
</template>

<script>
Expand All @@ -19,4 +20,8 @@ export default {
li {
margin-left: 1.25em;
}

.markdown-tool >>> a {
text-decoration: underline;
}
</style>
117 changes: 95 additions & 22 deletions src/components/RecipeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
</p>

<VueShowdown
:markdown="recipe.description"
:markdown="parsedDescription"
class="markdown-description"
/>
<p v-if="$store.state.recipe.url">
Expand Down Expand Up @@ -95,12 +95,12 @@
<section>
<aside>
<section>
<h3 v-if="recipe.ingredients.length">
<h3 v-if="parsedIngredients.length">
{{ t("cookbook", "Ingredients") }}
</h3>
<ul v-if="recipe.ingredients.length">
<ul v-if="parsedIngredients.length">
<RecipeIngredient
v-for="(ingredient, idx) in recipe.ingredients"
v-for="(ingredient, idx) in parsedIngredients"
:key="'ingr' + idx"
:ingredient="ingredient"
:recipe-ingredients-have-subgroups="
Expand All @@ -111,12 +111,12 @@
</section>

<section>
<h3 v-if="recipe.tools.length">
<h3 v-if="parsedTools.length">
{{ t("cookbook", "Tools") }}
</h3>
<ul v-if="recipe.tools.length">
<ul v-if="parsedTools.length">
<RecipeTool
v-for="(tool, idx) in recipe.tools"
v-for="(tool, idx) in parsedTools"
:key="'tool' + idx"
:tool="tool"
/>
Expand Down Expand Up @@ -252,11 +252,11 @@
</ul>
</section>
</aside>
<main v-if="recipe.instructions.length">
<main v-if="parsedInstructions.length">
<h3>{{ t("cookbook", "Instructions") }}</h3>
<ol class="instructions">
<RecipeInstruction
v-for="(instruction, idx) in recipe.instructions"
v-for="(instruction, idx) in parsedInstructions"
:key="'instr' + idx"
:instruction="instruction"
/>
Expand All @@ -272,6 +272,7 @@ import moment from "@nextcloud/moment"

import api from "cookbook/js/api-interface"
import helpers from "cookbook/js/helper"
import normalizeMarkdown from "cookbook/js/title-rename"

import RecipeImages from "./RecipeImages.vue"
import RecipeIngredient from "./RecipeIngredient.vue"
Expand Down Expand Up @@ -311,6 +312,10 @@ export default {
data() {
return {
headerPrefix: "## ",
parsedDescription: "",
parsedIngredients: [],
parsedInstructions: [],
parsedTools: [],
}
},
computed: {
Expand All @@ -329,22 +334,27 @@ export default {
nutrition: null,
}

if (this.$store.state.recipe === null) {
// console.log("Recipe is null")
return recipe
}

if (this.$store.state.recipe.description) {
recipe.description = this.convertRecipeReferences(
window.escapeHTML(this.$store.state.recipe.description)
recipe.description = helpers.escapeHTML(
this.$store.state.recipe.description
)
}

if (this.$store.state.recipe.recipeIngredient) {
recipe.ingredients = Object.values(
this.$store.state.recipe.recipeIngredient
).map((i) => this.convertRecipeReferences(window.escapeHTML(i)))
).map((i) => helpers.escapeHTML(i))
}

if (this.$store.state.recipe.recipeInstructions) {
recipe.instructions = Object.values(
this.$store.state.recipe.recipeInstructions
).map((i) => this.convertRecipeReferences(window.escapeHTML(i)))
).map((i) => helpers.escapeHTML(i))
}

if (this.$store.state.recipe.keywords) {
Expand Down Expand Up @@ -385,7 +395,7 @@ export default {

if (this.$store.state.recipe.tool) {
recipe.tools = this.$store.state.recipe.tool.map((i) =>
this.convertRecipeReferences(window.escapeHTML(i))
helpers.escapeHTML(i)
)
}

Expand Down Expand Up @@ -453,6 +463,77 @@ export default {
)
},
},
watch: {
recipe(r) {
// console.log('Recipe has been updated')
if (r) {
// console.log("Recipe", r)

if (r.description) {
this.parsedDescription = t("cookbook", "Loading…")
normalizeMarkdown(r.description).then((x) => {
this.parsedDescription = x
})
} else {
this.parsedDescription = ""
}

if (r.ingredients) {
this.parsedIngredients = r.ingredients.map(() =>
t("cookbook", "Loading…")
)
r.ingredients.forEach((ingredient, idx) => {
normalizeMarkdown(ingredient)
.then((x) => {
this.parsedIngredients.splice(idx, 1, x)
})
.catch((ex) => {
// eslint-disable-next-line no-console
console.log(ex)
})
})
} else {
this.parsedIngredients = []
}

if (r.instructions) {
this.parsedInstructions = r.instructions.map(() =>
t("cookbook", "Loading…")
)
r.instructions.forEach((instruction, idx) => {
normalizeMarkdown(instruction)
.then((x) => {
this.parsedInstructions.splice(idx, 1, x)
})
.catch((ex) => {
// eslint-disable-next-line no-console
console.log(ex)
})
})
} else {
this.parsedInstructions = []
}

if (r.tools) {
this.parsedTools = r.tools.map(() =>
t("cookbook", "Loading…")
)
r.tools.forEach((tool, idx) => {
normalizeMarkdown(tool)
.then((x) => {
this.parsedTools.splice(idx, 1, x)
})
.catch((ex) => {
// eslint-disable-next-line no-console
console.log(ex)
})
})
} else {
this.parsedTools = []
}
}
},
},
mounted() {
this.setup()
// Register data load method hook for access from the controls components
Expand All @@ -462,14 +543,6 @@ export default {
})
},
methods: {
convertRecipeReferences(text) {
const re = /(^|\s|[,._+&?!-])#r\/(\d+)(?=$|\s|[.,_+&?!-])/g
const converted = text.replace(
re,
`$1<a class="recipe-reference-inline" href="${this.$window.baseUrl}/#/recipe/$2">#$2</a>`
)
return converted
},
isNullOrEmpty(str) {
return !str || (typeof str === "string" && str.trim().length === 0)
},
Expand Down
3 changes: 0 additions & 3 deletions src/guest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { generateUrl } from "@nextcloud/router"

import Vue from "vue"

import api from "cookbook/js/api-interface"

import store from "./store"

import AppInvalidGuest from "./components/AppInvalidGuest.vue"
Expand All @@ -30,7 +28,6 @@ if (__webpack_use_dev_server__ || false) {

// eslint-disable-next-line no-param-reassign
window.baseUrl = generateUrl("apps/cookbook")
api.init(window.baseUrl)

// Also make the injections available in Vue components
Vue.prototype.$window = window
Expand Down
8 changes: 2 additions & 6 deletions src/js/api-interface.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import axios from "@nextcloud/axios"

let baseUrl
// const baseUrl = globalThis.window.baseUrl
import { generateUrl } from "@nextcloud/router"

function init($baseurl) {
baseUrl = $baseurl
}
const baseUrl = generateUrl("apps/cookbook")

function createNewRecipe(recipe) {
return axios({
Expand Down Expand Up @@ -107,7 +104,6 @@ function reindex() {
}

export default {
init,
recipes: {
create: createNewRecipe,
getAll: getAllRecipes,
Expand Down
85 changes: 85 additions & 0 deletions src/js/title-rename.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import api from "cookbook/js/api-interface"

import { generateUrl } from "@nextcloud/router"

const baseUrl = generateUrl("apps/cookbook")

function extractAllRecipeLinkIds(content) {
const re = /(?:^|[^#])#r\/([0-9]+)/g
let ret = []
let matches
for (
matches = re.exec(content);
matches !== null;
matches = re.exec(content)
) {
ret.push(matches[1])
}

// Make the ids unique, see https://stackoverflow.com/a/14438954/882756
function onlyUnique(value, index, self) {
return self.indexOf(value) === index
}
ret = ret.filter(onlyUnique)

return ret
}

async function getRecipesFromLinks(linkIds) {
return Promise.all(
linkIds.map(async (x) => {
let recipe
try {
recipe = await api.recipes.get(x)
} catch (ex) {
recipe = null
}
return recipe
})
)
}

function cleanUpRecipeList(recipes) {
return recipes.filter((r) => r !== null).map((x) => x.data)
}

function getRecipeUrl(id) {
return `${baseUrl}/#/recipe/${id}`
}

function insertMarkdownLinks(content, recipes) {
let ret = content
recipes.forEach((r) => {
const { id } = r

// Replace link urls in dedicated links (like [this example](#r/123))
ret = ret.replace(`](${id})`, `](${getRecipeUrl(id)})`)

// Replace plain references with recipe name
const rePlain = RegExp(
`(^|\\s|[,._+&?!-])#r/${id}($|\\s|[,._+&?!-])`,
"g"
)
// const re = /(^|\s|[,._+&?!-])#r\/(\d+)(?=$|\s|[.,_+&?!-])/g
ret = ret.replace(
rePlain,
`$1[${r.name} (\\#r/${id})](${getRecipeUrl(id)})$2`
)
})
return ret
}

async function normalizeNamesMarkdown(content) {
// console.log(`Content: ${content}`)
const linkIds = extractAllRecipeLinkIds(content)
let recipes = await getRecipesFromLinks(linkIds)
recipes = cleanUpRecipeList(recipes)
// console.log("List of recipes", recipes)

const markdown = insertMarkdownLinks(content, recipes)
// console.log("Formatted markdown:", markdown)

return markdown
}

export default normalizeNamesMarkdown
2 changes: 0 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { generateUrl } from "@nextcloud/router"

import Vue from "vue"

import api from "cookbook/js/api-interface"
import helpers from "cookbook/js/helper"

import router from "./router"
Expand All @@ -35,7 +34,6 @@ if (__webpack_use_dev_server__ || false) {

// eslint-disable-next-line no-param-reassign
window.baseUrl = generateUrl("apps/cookbook")
api.init(window.baseUrl)

helpers.useRouter(router)

Expand Down